@@ -12,6 +12,8 @@ JSONRPC.Utils = require("./Utils");
1212
1313const EventEmitter = require ( "events" ) ;
1414
15+ const WebSocket = require ( "ws" ) ;
16+
1517
1618/**
1719 * The "madeReverseCallsClient" event offers automatically instantiated API clients (API clients are instantiated for each connection, lazily).
@@ -57,7 +59,7 @@ class BidirectionalWebsocketRouter extends EventEmitter
5759 */
5860 async addWebSocket ( webSocket )
5961 {
60- if ( webSocket . readyState === webSocket . constructor . CLOSED )
62+ if ( webSocket . readyState === WebSocket . CLOSED )
6163 {
6264 // WebSocket.CLOSING should be followed by a closed event.
6365 // WebSocket.OPEN is desired.
@@ -81,33 +83,37 @@ class BidirectionalWebsocketRouter extends EventEmitter
8183
8284 this . _objSessions [ nWebSocketConnectionID ] = objSession ;
8385
84- webSocket . on (
86+ webSocket . addEventListener (
8587 "close" ,
86- ( code , message ) => {
88+ ( closeEvent ) => {
89+ //closeEvent.code;
90+ //closeEvent.reason;
91+ //closeEvent.wasClean;
92+
8793 delete this . _objSessions [ nWebSocketConnectionID ] ;
8894 }
8995 ) ;
9096
91- webSocket . on (
97+ webSocket . addEventListener (
9298 "error" ,
9399 ( error ) => {
94100 delete this . _objSessions [ nWebSocketConnectionID ] ;
95101
96- if ( webSocket . readyState === webSocket . constructor . OPEN )
102+ if ( webSocket . readyState === WebSocket . OPEN )
97103 {
98104 webSocket . close (
99- /* CloseEvent.Internal Error */ 1011 ,
105+ /*CLOSE_NORMAL*/ 1000 , // Chrome only supports 1000 or the 3000-3999 range ///* CloseEvent.Internal Error */ 1011,
100106 error . message
101107 ) ;
102108 }
103109 }
104110 ) ;
105111
106- webSocket . on (
112+ webSocket . addEventListener (
107113 "message" ,
108- async ( strMessage ) =>
114+ async ( messageEvent ) =>
109115 {
110- await this . _routeMessage ( strMessage , objSession ) ; //.then(() => {}).catch(console.error);
116+ await this . _routeMessage ( messageEvent . data , objSession ) ; //.then(() => {}).catch(console.error);
111117 }
112118 ) ;
113119
@@ -222,7 +228,7 @@ class BidirectionalWebsocketRouter extends EventEmitter
222228
223229 console . log ( "[" + process . pid + "] Unclean state. Unable to match WebSocket message to an existing Promise or qualify it as a request or response." ) ;
224230 webSocket . close (
225- /* CloseEvent.Internal Error */ 1011 ,
231+ /*CLOSE_NORMAL*/ 1000 , // Chrome only supports 1000 or the 3000-3999 range ///* CloseEvent.Internal Error */ 1011,
226232 "Unclean state. Unable to match WebSocket message to an existing Promise or qualify it as a request or response."
227233 ) ;
228234
@@ -235,7 +241,7 @@ class BidirectionalWebsocketRouter extends EventEmitter
235241 {
236242 if ( ! this . _jsonrpcServer )
237243 {
238- if ( webSocket . readyState === webSocket . constructor . OPEN )
244+ if ( webSocket . readyState === WebSocket . OPEN )
239245 {
240246 webSocket . send ( JSON . stringify ( {
241247 id : null ,
@@ -290,7 +296,7 @@ class BidirectionalWebsocketRouter extends EventEmitter
290296
291297 await this . _jsonrpcServer . processRequest ( incomingRequest ) ;
292298
293- if ( webSocket . readyState !== webSocket . constructor . OPEN )
299+ if ( webSocket . readyState !== WebSocket . OPEN )
294300 {
295301 console . error ( "webSocket.readyState: " + JSON . stringify ( webSocket . readyState ) + ". Request was " + strMessage + ". Attempted responding with " + JSON . stringify ( incomingRequest . callResultToBeSerialized , undefined , "\t" ) + "." ) ;
296302 }
@@ -306,7 +312,7 @@ class BidirectionalWebsocketRouter extends EventEmitter
306312 {
307313 if ( ! this . _jsonrpcServer )
308314 {
309- if ( webSocket . readyState === webSocket . constructor . OPEN )
315+ if ( webSocket . readyState === WebSocket . OPEN )
310316 {
311317 webSocket . send ( JSON . stringify ( {
312318 id : null ,
@@ -319,10 +325,10 @@ class BidirectionalWebsocketRouter extends EventEmitter
319325 }
320326 }
321327
322- if ( webSocket . readyState === webSocket . constructor . OPEN )
328+ if ( webSocket . readyState === WebSocket . OPEN )
323329 {
324330 webSocket . close (
325- /* CloseEvent.Internal Error */ 1011 ,
331+ /*CLOSE_NORMAL*/ 1000 , // Chrome only supports 1000 or the 3000-3999 range ///* CloseEvent.Internal Error */ 1011,
326332 "How can the client be not initialized, and yet getting responses from phantom requests? Closing websocket."
327333 ) ;
328334 }
@@ -355,7 +361,7 @@ class BidirectionalWebsocketRouter extends EventEmitter
355361 && this . _objSessions [ nWebSocketConnectionID ] . clientWebSocketTransportPlugin === null
356362 )
357363 {
358- if ( webSocket . readyState === webSocket . constructor . OPEN )
364+ if ( webSocket . readyState === WebSocket . OPEN )
359365 {
360366 webSocket . send ( JSON . stringify ( {
361367 id : null ,
@@ -368,11 +374,11 @@ class BidirectionalWebsocketRouter extends EventEmitter
368374 }
369375 }
370376
371- if ( webSocket . readyState === webSocket . constructor . OPEN )
377+ if ( webSocket . readyState === WebSocket . OPEN )
372378 {
373379 console . log ( "[" + process . pid + "] Unclean state. Closing websocket." ) ;
374380 webSocket . close (
375- /* CloseEvent.Internal Error */ 1011 ,
381+ /*CLOSE_NORMAL*/ 1000 , // Chrome only supports 1000 or the 3000-3999 range ///* CloseEvent.Internal Error */ 1011,
376382 "Unclean state. Closing websocket."
377383 ) ;
378384 }
0 commit comments