committed by
Diamond Lewis
parent
d4fa62ae26
commit
bde96a9002
@@ -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() {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user