// Let us open a web socketconstws=newWebSocket(address);// event when connection to websocket is establishedws.onopen=function(){// ...};// event when websocket receives new interactionsws.onmessage=function(evt){// evt.data contains interactions from ReFlex frameworkconstreceived_interactions=evt.data;// handle interactions update};// event when websocket connection is terminatedws.onclose=function(){// websocket is closed.};// event for websocket error handlingws.onerror=function(error){// handle error (log, reconnect, ...)}
// open the websocketws=newWebSocket(address);// event when connection to websocket is establishedws.onopen=function(){constconn_msg="Successfully connected to "+address;// remove button and display connection stateshowWebSocketState(conn_msg,true);};// event when websocket receives new interactionsws.onmessage=function(evt){// update message numbermsgId+=1;// parse dataconstdata=JSON.parse(evt.data);// display formatted messageshowMessages(msgId,data);// visualize messages on canvasshowInteractions(data);// send interactions to logging serverif(logMessages){// log each interaction as separate data setconstinteractions=extractInteractions(data);interactions.forEach((msg)=>fetch(`${logAddress}log/data`,{// build messagemethod:"POST",body:JSON.stringify({message:msg}),headers:{"Content-type":"application/json; charset=UTF-8"}}));}};// event when websocket connection is terminatedws.onclose=function(){constconn_msg="Connection is closed...";// update button and connection stateshowWebSocketState(conn_msg,false);};// event for websocket error handlingws.onerror=function(error){constconn_msg=`Connection Error: ${error}`;// update button and connection stateshowWebSocketState(conn_msg,false);}