Tag: Clear string Reconnect Eve item Check URL pen tla
The client is implemented with react,
Key Code Websoket
classWS {constructor () { This. WS =NULL; This. Openhandler =NULL; This. MessageHandler =NULL; This. ErrorHandler =NULL; This. Closehandler =NULL; This. RequestID =0; This. MessageQueue = []; This. Msgchecktimer = SetInterval ( This. Ontimer.bind ( This), +); This. Timeout =15000; } onTimer () {Let istimeout=false; Let now=Date.now (); for(Let i =0; I < This. messagequeue.length; i++) {Let msg= This. Messagequeue[i]; Let Deltatime= Msg.time-Now ; if(Deltatime > This. TimeOut) {Istimeout=true; Break; } } if(istimeout) This. Close (); } open (URL) {console.log ('Open') This. WS =NewWebSocket (URL); This. Ws.onopen = function (Event) {Console.log ('open-->',Event) if( This. openhandler! =NULL) This. Openhandler (Event); }.bind ( This) This. ws.onmessage = function (Event) {Console.log ('OnMessage') if( This. MessageHandler! =NULL) This. MessageHandler (Event); }.bind ( This); This. Ws.onerror = function (Event) {Console.log ('onerror-->',Event) This. Close (); if( This. ErrorHandler! =NULL) This. ErrorHandler (Event); }.bind ( This); This. Ws.onclose = function (Event) {Console.log ('onclose-->',Event) This. Close (); if( This. closehandler! =NULL) This. Closehandler (Event); }.bind ( This); } close () {if( This. ws! =NULL) { Try { This. Ws.close (); } Catch(e) {} This. WS =NULL; This. MessageQueue = []; } if( This. msgchecktimer! =NULL) {clearinterval ( This. Msgchecktimer); This. Msgchecktimer =NULL; }} makemsg (commandId) {Let JSON= {}; Json.account= Sessionstorage.getitem (' Account'); Json.token= Sessionstorage.getitem ('token'); Json.commandid=commandId; Json.requestid= This. requestid++; Json.packtype=Packtype.requestresponse; Json.errorcode=Errorcode.ok; returnJSON; } Send (CommandId, submsg, callback) {if( This. ws! =NULL) {Let msg= This. makemsg (COMMANDID); if(Submsg! =NULL) Msg.submsgdata=json.stringify (submsg); Let SS=json.stringify (msg); This. Ws.send (ss); Msg.time=Date.now (); Msg.callback=callback; This. Messagequeue.push (msg); }} onresponse (responsemsg) { for(Let i =0; I < This. messagequeue.length; i++) {Let msg= This. Messagequeue[i]; if(Msg.requestid = =Responsemsg.requestid) { This. Messagequeue.splice (I,1); if(Msg.callback! =undefined) msg.callback (responsemsg); Break; } } }}
openws () {varURL ="ws://"+ Wsads +"/ws/crmclient"; if( This. WS = =NULL) { This. WS =NewWS () This. Ws.openhandler = function (Event) { This. SetState ({wsopensuccess:true }) This. Ws.send (Msgcommand.logincommand,NULL, This. Getallplayers.bind ( This)); }.bind ( This) This. Ws.messagehandler = function (Event) { This. OnMessage (Event)}.bind ( This) This. Ws.errorhandler =function () { This. SetState ({iserr:true, Ntstype:'reconnection', NTSTEXT:TEXTLAND.OHERR}) }.bind ( This) This. Ws.closehandler =function () { This. SetState ({iserr:true, Ntstype:'reconnection', NtsText:TextLand.OhDisconnect}) }.bind ( This) } Else { This. Ws.close ()} This. Ws.open (URL)},
58.websoket implementation of the customer service implementation of the text messages and pictures