import { RouterService } from "./router.service" ; import { CONFIG } from "./../../config" ; export namespace SocketService { let socket ; let idCounter = 0 ; let sendCallbacks = { } ; export function init ( ) { socket = new WebSocket ( CONFIG . server_endpoint ) ; socket . onmessage = handleMessage ; return socket ; } function createMsgID ( ) { idCounter ++ ; if ( idCounter > 9999999 ) { idCounter = 0 ; } return "client_" + idCounter . toString ( ) ; } export function send ( type , msg = "" , callback = ( ) => { } ) { let id = createMsgID ( ) ; sendCallbacks [ id ] = callback ; let json = JSON . stringify ( { type , msg , id , origin : "client" } ) ; socket . send ( json ) ; } function handleMessage ( e ) { let data = JSON . parse ( e . data ) ; if ( data . origin === "client" && sendCallbacks [ data . id ] !== undefined ) { sendCallbacks [ data . id ] ( data ) ; } else { RouterService . routeMessages ( data ) ; } } }