99import * as misc from './misc' ;
1010import { getCoreDir , runPIOCommand } from './core' ;
1111
12+ import WebSocket from 'ws' ; // eslint-disable-line import/no-unresolved
1213import crypto from 'crypto' ;
1314import fs from 'fs' ;
1415import got from 'got' ;
1516import jsonrpc from 'jsonrpc-lite' ;
1617import path from 'path' ;
1718import qs from 'querystringify' ;
1819import tcpPortUsed from 'tcp-port-used' ;
19- import ws from 'ws' ;
2020
2121const SERVER_LAUNCH_TIMEOUT = 30 ; // 30 seconds
2222const 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
116112async function isPortUsed ( host , port ) {
0 commit comments