Skip to content

Commit 7958b38

Browse files
committed
Migrate to WebSocket 8.0
1 parent 3e928b1 commit 7958b38

File tree

2 files changed

+13
-17
lines changed

2 files changed

+13
-17
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
"tar": "~6.1.11",
6060
"tcp-port-used": "~1.0.2",
6161
"tmp": "~0.2.1",
62-
"ws": "~7.4.6"
62+
"ws": "~8.2.3"
6363
},
6464
"devDependencies": {
6565
"@babel/core": "~7.16.0",

src/home.js

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
import * as misc from './misc';
1010
import { getCoreDir, runPIOCommand } from './core';
1111

12+
import WebSocket from 'ws'; // eslint-disable-line import/no-unresolved
1213
import crypto from 'crypto';
1314
import fs from 'fs';
1415
import got from 'got';
1516
import jsonrpc from 'jsonrpc-lite';
1617
import path from 'path';
1718
import qs from 'querystringify';
1819
import tcpPortUsed from 'tcp-port-used';
19-
import ws from 'ws';
2020

2121
const SERVER_LAUNCH_TIMEOUT = 30; // 30 seconds
2222
const SERVER_AUTOSHUTDOWN_TIMEOUT = 3600; // 1 hour
@@ -70,30 +70,26 @@ async function listenIDECommands(callback) {
7070
if (_IDECMDS_LISTENER_STATUS > 0) {
7171
return;
7272
}
73-
const sock = new ws(constructServerUrl({ scheme: 'ws', path: '/wsrpc' }), {
73+
const ws = new WebSocket(constructServerUrl({ scheme: 'ws', path: '/wsrpc' }), {
7474
perMessageDeflate: false,
7575
});
76-
sock.onopen = () => {
76+
ws.on('open', () => {
7777
_IDECMDS_LISTENER_STATUS = 1;
7878
// "ping" message to initiate 'ide.listen_commands'
79-
sock.send(
80-
JSON.stringify(jsonrpc.request(Math.random().toString(), 'core.version'))
81-
);
82-
};
83-
84-
sock.onclose = () => {
79+
ws.send(JSON.stringify(jsonrpc.request(Math.random().toString(), 'core.version')));
80+
});
81+
ws.on('close', () => {
8582
_IDECMDS_LISTENER_STATUS = 0;
86-
};
87-
88-
sock.onmessage = async (event) => {
83+
});
84+
ws.on('message', async (data) => {
8985
try {
90-
const msg = jsonrpc.parse(event.data);
86+
const msg = jsonrpc.parse(data);
9187
if (msg.type === 'success' && msg.payload.result.method) {
9288
const result = await callback(
9389
msg.payload.result.method,
9490
msg.payload.result.params
9591
);
96-
sock.send(
92+
ws.send(
9793
JSON.stringify(
9894
jsonrpc.request(Math.random().toString(), 'ide.on_command_result', [
9995
msg.payload.result.id,
@@ -107,10 +103,10 @@ async function listenIDECommands(callback) {
107103
} catch (err) {
108104
console.error('Invalid RPC message: ', err);
109105
}
110-
sock.send(
106+
ws.send(
111107
JSON.stringify(jsonrpc.request(Math.random().toString(), 'ide.listen_commands'))
112108
);
113-
};
109+
});
114110
}
115111

116112
async function isPortUsed(host, port) {

0 commit comments

Comments
 (0)