forked from cerebral/overmind
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionManager.js
More file actions
40 lines (36 loc) · 1.04 KB
/
ConnectionManager.js
File metadata and controls
40 lines (36 loc) · 1.04 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
const WebSocketServer = require('ws').Server
class ConnectionManager {
constructor(mainWindow) {
this.mainWindow = mainWindow
this.connect = this.connect.bind(this)
this.onDevtoolsMessage = this.onDevtoolsMessage.bind(this)
this.onConnection = this.onConnection.bind(this)
this.onError = this.onError.bind(this)
}
onError() {
this.mainWindow.webContents.send('port:exists')
}
onConnection(ws) {
this.ws = ws
this.ws.on('message', this.onAppMessage.bind(this))
}
onAppMessage(message) {
const parsedMessage = JSON.parse(message)
this.mainWindow.webContents.send('message', parsedMessage)
}
onDevtoolsMessage(_, payload) {
if (!this.clients[payload.port] || !this.clients[payload.port].ws) {
return
}
this.ws.send(JSON.stringify(payload))
}
connect(_, port) {
if (this.wss) {
return
}
this.wss = new WebSocketServer({ port: Number(port) })
this.wss.on('connection', this.onConnection)
this.wss.on('error', this.onError)
}
}
module.exports = ConnectionManager