Fix live query (#5871)

* Fix live query

* fix test
This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2019-07-30 20:40:54 -07:00
committed by Diamond Lewis
parent d4fa62ae26
commit bde96a9002
2 changed files with 11 additions and 16 deletions

View File

@@ -8,14 +8,16 @@ const WebSocketServer = require('ws').Server;
export class WSAdapter extends WSSAdapter { export class WSAdapter extends WSSAdapter {
constructor(options: any) { constructor(options: any) {
super(options); super(options);
const wss = new WebSocketServer({ server: options.server }); this.options = options;
wss.on('listening', this.onListen);
wss.on('connection', this.onConnection);
} }
onListen() {} onListen() {}
onConnection(ws) {} onConnection(ws) {}
start() {} start() {
const wss = new WebSocketServer({ server: this.options.server });
wss.on('listening', this.onListen);
wss.on('connection', this.onConnection);
}
close() {} close() {}
} }

View File

@@ -6,21 +6,13 @@ import events from 'events';
export class ParseWebSocketServer { export class ParseWebSocketServer {
server: Object; server: Object;
constructor( constructor(server: any, onConnect: Function, config) {
server: any,
onConnect: Function,
config
) {
config.server = server; config.server = server;
const wss = loadAdapter( const wss = loadAdapter(config.wssAdapter, WSAdapter, config);
config.wssAdapter,
WSAdapter,
config,
);
wss.onListen = () => { wss.onListen = () => {
logger.info('Parse LiveQuery Server starts running'); logger.info('Parse LiveQuery Server starts running');
}; };
wss.onConnection = (ws) => { wss.onConnection = ws => {
onConnect(new ParseWebSocket(ws)); onConnect(new ParseWebSocket(ws));
// Send ping to client periodically // Send ping to client periodically
const pingIntervalId = setInterval(() => { const pingIntervalId = setInterval(() => {
@@ -47,7 +39,8 @@ export class ParseWebSocket extends events.EventEmitter {
constructor(ws: any) { constructor(ws: any) {
super(); super();
ws.onmessage = (request) => this.emit('message', request); ws.onmessage = request =>
this.emit('message', request && request.data ? request.data : request);
ws.onclose = () => this.emit('disconnect'); ws.onclose = () => this.emit('disconnect');
this.ws = ws; this.ws = ws;
} }