|
1 | 1 | const WebSocket = require('ws') |
2 | 2 |
|
| 3 | + |
| 4 | + |
3 | 5 | class DevtoolBackend { |
4 | 6 | constructor(options) { |
5 | 7 | this.options = options |
6 | | - |
7 | 8 | this.onError = this.onError.bind(this) |
| 9 | + |
8 | 10 | this.onClientConnection = this.onClientConnection.bind(this) |
9 | 11 | this.onDevtoolConnection = this.onDevtoolConnection.bind(this) |
| 12 | + this.onConnection = this.onConnection.bind(this) |
10 | 13 | this.onClientMessage = this.onClientMessage.bind(this) |
11 | 14 | this.onDevtoolMessage = this.onDevtoolMessage.bind(this) |
12 | | - |
| 15 | + |
13 | 16 | this.devtoolServer = new WebSocket.Server( |
14 | 17 | { |
15 | 18 | port: options.port, |
16 | 19 | }, |
17 | 20 | () => {} |
18 | 21 | ) |
19 | 22 |
|
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 | + } |
21 | 34 | } |
| 35 | + |
22 | 36 | onDevtoolConnection(ws) { |
23 | 37 | this.devtoolSocket = ws |
24 | 38 | this.devtoolSocket.on('message', this.onDevtoolMessage) |
@@ -46,14 +60,16 @@ class DevtoolBackend { |
46 | 60 | this.options.onRelaunch() |
47 | 61 | break |
48 | 62 | 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) |
50 | 65 | break |
51 | 66 | default: |
52 | 67 | this.clientSocket.send(JSON.stringify(parsedMessage.data)) |
53 | 68 | } |
54 | 69 | } |
55 | 70 | onError() { |
56 | | - this.devtoolServer.emit('port:exists') |
| 71 | + console.log('TODO: onError'); |
| 72 | + // this.devtoolServer.emit('port:exists') |
57 | 73 | } |
58 | 74 | onClientConnection(ws) { |
59 | 75 | this.clientSocket = ws |
@@ -82,23 +98,6 @@ class DevtoolBackend { |
82 | 98 | ) |
83 | 99 | } |
84 | 100 | } |
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 | | - } |
102 | 101 | } |
103 | 102 |
|
104 | 103 | DevtoolBackend.create = (options) => new DevtoolBackend(options) |
|
0 commit comments