@ -7,49 +7,33 @@
/* eslint no-console: "off" */
const PROTOCOL = window . location . protocol === "http:" ? "ws" : "ws s";
import { REFORIS _URL _PREFIX } from "../utils/forisUrl s";
const URL = process . env . LIGHTTPD
? ` ${ PROTOCOL } :// ${ window . location . host } / ${
process . env . WSPATH || "foris-ws"
} `
: ` ${ PROTOCOL } :// ${ window . location . hostname } :9081 ` ;
const WAITING _FOR _CONNECTION _TIMEOUT = 500 ;
const { io } = require ( "socket.io-client" ) ;
export class WebSockets {
constructor ( ) {
this . ws = new WebSocket ( URL ) ;
this . ws . onerror = ( e ) => {
console . error ( "WS: Error:" , e ) ;
} ;
this . ws . onmessage = ( e ) => {
console . debug ( ` WS: Received Message: ${ e . data } ` ) ;
const data = JSON . parse ( e . data ) ;
this . dispatch ( data ) ;
} ;
this . ws . onopen = ( ) => {
console . debug ( "WS: Connection open." ) ;
} ;
this . ws . onclose = ( ) => {
console . debug ( "WS: Connection closed." ) ;
} ;
this . socket = io ( "/notifications" , {
path : ` ${ REFORIS _URL _PREFIX } /reforis-ws ` ,
} ) ;
this . connection = null ;
this . socket . on ( "disconnect" , ( reason ) => {
this . connection = null ;
console . debug ( ` SocketIO disconnected ( ${ reason } ) ` ) ;
} ) ;
this . socket . on ( "notification" , ( message ) => {
console . debug ( "WS: Received Message:" , message ) ;
this . dispatch ( message ) ;
} ) ;
this . socket . on ( "connect" , ( connection ) => {
this . connection = connection ;
console . debug ( ` SocketIO connected. ` ) ;
} ) ;
// callbacks[module][action]
this . callbacks = { } ;
}
waitForConnection ( callback ) {
if ( this . ws . readyState === 1 ) {
callback ( ) ;
} else {
const that = this ;
setTimeout ( ( ) => {
that . waitForConnection ( callback ) ;
} , WAITING _FOR _CONNECTION _TIMEOUT ) ;
}
}
bind ( module , action , callback ) {
this . callbacks [ module ] = this . callbacks [ module ] || { } ;
this . callbacks [ module ] [ action ] = this . callbacks [ module ] [ action ] || [ ] ;
@ -57,13 +41,6 @@ export class WebSockets {
return this ;
}
subscribe ( module ) {
this . waitForConnection ( ( ) => {
this . send ( "subscribe" , module ) ;
} ) ;
return this ;
}
unbind ( module , action , callback ) {
const callbacks = this . callbacks [ module ] [ action ] ;
@ -77,25 +54,9 @@ export class WebSockets {
}
if ( Object . keys ( this . callbacks [ module ] ) . length === 0 ) {
this . unsubscribe ( module ) ;
}
return this ;
}
unsubscribe ( module ) {
this . waitForConnection ( ( ) => {
this . send ( "unsubscribe" , module ) ;
delete this . callbacks [ module ] ;
} ) ;
return this ;
}
}
send ( action , params ) {
const payload = JSON . stringify ( { action , params } ) ;
this . waitForConnection ( ( ) => {
this . ws . send ( payload ) ;
} ) ;
return this ;
}
@ -107,18 +68,15 @@ export class WebSockets {
chain = this . callbacks [ json . module ] [ json . action ] ;
} catch ( error ) {
if ( error instanceof TypeError ) {
console . warn (
` Callback for this message wasn't found: ${ error . data } `
console . debug (
` Callbacks for this module wasn't found: ${ json . module } `
) ;
} else throw error ;
}
if ( typeof chain === "undefined" ) return ;
console . debug ( "Handling WS message" , json ) ;
chain . forEach ( ( callback ) => callback ( json ) ) ;
}
close ( ) {
this . ws . close ( ) ;
}
}