-
-
Notifications
You must be signed in to change notification settings - Fork 793
/
WebSocketServer.js
44 lines (33 loc) · 1.12 KB
/
WebSocketServer.js
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 { Server } from 'ws'
import debugLog from '../../debugLog.js'
import serverlessLog from '../../serverlessLog.js'
import { createUniqueId } from '../../utils/index.js'
export default class WebSocketServer {
constructor(options, webSocketClients, sharedServer) {
this._options = options
this._server = new Server({
server: sharedServer,
})
this._webSocketClients = webSocketClients
this._server.on('connection', (webSocketClient, request) => {
console.log('received connection')
const connectionId = createUniqueId()
debugLog(`connect:${connectionId}`)
this._webSocketClients.addClient(webSocketClient, request, connectionId)
})
}
async start() {
const { host, httpsProtocol, websocketPort } = this._options
serverlessLog(
`Offline [websocket] listening on ws${
httpsProtocol ? 's' : ''
}://${host}:${websocketPort}`,
)
}
// no-op, we're re-using the http server
stop() {}
addRoute(functionKey, functionObj, route) {
this._webSocketClients.addRoute(functionKey, functionObj, route)
// serverlessLog(`route '${route}'`)
}
}