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 {
constructor(options: any) {
super(options);
const wss = new WebSocketServer({ server: options.server });
wss.on('listening', this.onListen);
wss.on('connection', this.onConnection);
this.options = options;
}
onListen() {}
onConnection(ws) {}
start() {}
start() {
const wss = new WebSocketServer({ server: this.options.server });
wss.on('listening', this.onListen);
wss.on('connection', this.onConnection);
}
close() {}
}

View File

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