forked from parse-community/parse-server
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathParseWebSocketServer.js
More file actions
44 lines (37 loc) · 1.06 KB
/
ParseWebSocketServer.js
File metadata and controls
44 lines (37 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import PLog from './PLog';
let typeMap = new Map([['disconnect', 'close']]);
export class ParseWebSocketServer {
server: Object;
constructor(server: any, onConnect: Function, websocketTimeout: number = 10 * 1000) {
let WebSocketServer = require('ws').Server;
let wss = new WebSocketServer({ server: server });
wss.on('listening', () => {
PLog.log('Parse LiveQuery Server starts running');
});
wss.on('connection', (ws) => {
onConnect(new ParseWebSocket(ws));
// Send ping to client periodically
let pingIntervalId = setInterval(() => {
if (ws.readyState == ws.OPEN) {
ws.ping();
} else {
clearInterval(pingIntervalId);
}
}, websocketTimeout);
});
this.server = wss;
}
}
export class ParseWebSocket {
ws: any;
constructor(ws: any) {
this.ws = ws;
}
on(type: string, callback): void {
let wsType = typeMap.has(type) ? typeMap.get(type) : type;
this.ws.on(wsType, callback);
}
send(message: any, channel: string): void {
this.ws.send(message);
}
}