Skip to content

Commit 7f6a786

Browse files
committed
Enhance WebSocket server lifecycle management with automatic shutdown and secure local binding
- [Pasted text] ├── apps │ ├── browser │ │ ├── package.json │ │ ├── src │ │ │ ├── background │ │ │ │ ├── keep-alive.ts │ │ │ │ ├── main.ts │ │ │ │ ├── message-handler.ts │ │ │ │ └── websocket.ts │ │ │ └── manifest.json │ │ └── webpack.config.ts │ └── editor │ ├── package.json │ ├── src │ │ ├── extension.ts │ │ └── services │ │ ├── websocket-manager.ts │ │ └── websocket-server-process.ts │ └── webpack.config.ts └── packages └── shared └── src ├── constants │ └── websocket.ts └── types └── websocket-message.ts
1 parent 5b04bef commit 7f6a786

2 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎apps/editor/src/services/websocket-manager.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class WebSocketManager {
5555
resolve(false)
5656
})
5757
})
58-
.listen(port)
58+
.listen(port, '127.0.0.1')
5959
})
6060
}
6161

‎apps/editor/src/services/websocket-server-process.ts‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ class WebSocketServer {
2828
private connections: Set<WebSocket> = new Set()
2929
private server: http.Server
3030
private wss: any
31+
private shutdown_timer: NodeJS.Timeout | null = null
3132

3233
constructor() {
3334
this.server = this._create_http_server()
3435
this.wss = new WebSocket.Server({
3536
server: this.server,
37+
maxPayload: 10 * 1024 * 1024,
3638
verifyClient: (info: any, cb: any) => {
3739
const origin = info.req.headers.origin
3840
if (origin) {
@@ -48,10 +50,28 @@ class WebSocketServer {
4850
this._setup_websocket_server()
4951

5052
setInterval(() => this._ping_clients(), 10 * 1000)
53+
this._schedule_shutdown()
5154

5255
console.log(`Starting WebSocket server process (PID: ${process.pid})`)
5356
}
5457

58+
private _schedule_shutdown() {
59+
if (!this.shutdown_timer) {
60+
this.shutdown_timer = setTimeout(() => {
61+
if (this.vscode_clients.size == 0) {
62+
this._shutdown()
63+
}
64+
}, 10000)
65+
}
66+
}
67+
68+
private _cancel_shutdown() {
69+
if (this.shutdown_timer) {
70+
clearTimeout(this.shutdown_timer)
71+
this.shutdown_timer = null
72+
}
73+
}
74+
5575
private _create_http_server(): http.Server {
5676
return http.createServer((req: any, res: any) => {
5777
res.setHeader('Access-Control-Allow-Origin', '*')
@@ -107,6 +127,7 @@ class WebSocketServer {
107127

108128
this.connections.add(ws)
109129

130+
ws.on('error', (err: any) => console.error('WS Error:', err))
110131
ws.on('message', (message: any) => this._handle_message(message, ws))
111132
ws.on('close', () => this._handle_disconnection(ws, is_browser_client))
112133
}
@@ -134,6 +155,8 @@ class WebSocketServer {
134155
}
135156

136157
private _handle_vscode_connection(ws: WebSocket) {
158+
this._cancel_shutdown()
159+
137160
const client_id = this._generate_client_id()
138161
this.vscode_clients.set(client_id, { ws, client_id, is_alive: true })
139162

@@ -172,6 +195,10 @@ class WebSocketServer {
172195
return
173196
}
174197

198+
if (!msg_data || typeof msg_data != 'object') {
199+
return
200+
}
201+
175202
if (msg_data.action === 'pong') {
176203
for (const client of this.browser_clients.values()) {
177204
if (client.ws === ws) {
@@ -245,8 +272,8 @@ class WebSocketServer {
245272
}
246273
this.connections.delete(ws)
247274

248-
if (this.connections.size == 0) {
249-
this._shutdown()
275+
if (this.vscode_clients.size == 0) {
276+
this._schedule_shutdown()
250277
}
251278
}
252279

0 commit comments

Comments
 (0)