Skip to content

Commit df534ed

Browse files
committed
feat(overmind): use single WS server for devtools
1 parent 88f9385 commit df534ed

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

packages/node_modules/overmind-devtools-client/DevtoolBackend.js

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,38 @@
11
const WebSocket = require('ws')
22

3+
4+
35
class DevtoolBackend {
46
constructor(options) {
57
this.options = options
6-
78
this.onError = this.onError.bind(this)
9+
810
this.onClientConnection = this.onClientConnection.bind(this)
911
this.onDevtoolConnection = this.onDevtoolConnection.bind(this)
12+
this.onConnection = this.onConnection.bind(this)
1013
this.onClientMessage = this.onClientMessage.bind(this)
1114
this.onDevtoolMessage = this.onDevtoolMessage.bind(this)
12-
15+
1316
this.devtoolServer = new WebSocket.Server(
1417
{
1518
port: options.port,
1619
},
1720
() => {}
1821
)
1922

20-
this.devtoolServer.on('connection', this.onDevtoolConnection)
23+
this.devtoolServer.on('connection', this.onConnection)
24+
this.devtoolServer.on('error', this.onError)
25+
}
26+
27+
onConnection(ws, req) {
28+
// devtools connects with ?devtools=1 in url
29+
if (req.url.indexOf("devtools") !== -1) {
30+
this.onDevtoolConnection(ws, req)
31+
} else {
32+
this.onClientConnection(ws, req)
33+
}
2134
}
35+
2236
onDevtoolConnection(ws) {
2337
this.devtoolSocket = ws
2438
this.devtoolSocket.on('message', this.onDevtoolMessage)
@@ -46,14 +60,16 @@ class DevtoolBackend {
4660
this.options.onRelaunch()
4761
break
4862
case 'connect':
49-
this.createClientServer(parsedMessage.data)
63+
console.log('TODO: connect'); // TODO: Let devtools know that they are connected to server?
64+
// this.createClientServer(parsedMessage.data)
5065
break
5166
default:
5267
this.clientSocket.send(JSON.stringify(parsedMessage.data))
5368
}
5469
}
5570
onError() {
56-
this.devtoolServer.emit('port:exists')
71+
console.log('TODO: onError');
72+
// this.devtoolServer.emit('port:exists')
5773
}
5874
onClientConnection(ws) {
5975
this.clientSocket = ws
@@ -82,23 +98,6 @@ class DevtoolBackend {
8298
)
8399
}
84100
}
85-
createClientServer(port) {
86-
if (this.clientPort === Number(port)) {
87-
return
88-
}
89-
90-
if (this.clientServer) {
91-
this.clientServer.close()
92-
}
93-
94-
this.clientPort = Number(port)
95-
this.clientServer = new WebSocket.Server(
96-
{ port: this.clientPort },
97-
() => {}
98-
)
99-
this.clientServer.on('connection', this.onClientConnection)
100-
this.clientServer.on('error', this.onError)
101-
}
102101
}
103102

104103
DevtoolBackend.create = (options) => new DevtoolBackend(options)

0 commit comments

Comments
 (0)