| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- var wsc2={
- url : 'ws://127.0.0.1:9999',
- ws:null,
- is_onlien:false,
- timer_ping:null,
- timer_close:null,
- interval_ping:10000,
- interval_close:5000,
- init:function(){
- var self=this;
- self.ws= new WebSocket(self.url);
-
- self.ws.onopen = function(evt) {
- console.log("Connection open ...");
-
- self.is_onlien=true;
- self.ws.send("client connect success");
-
- start_heartbeat();
- };
-
- self.ws.onmessage = function(evt) {
- if(evt.data=="ping"){
- self.send("pong");
- console.log("on ping");
- }
- else if(evt.data=="pong"){
- console.log("on pong");
- }
- else{
- console.log( "Received Message: " + evt.data);
- }
-
- clearTimeout(self.timer_close);
- clearTimeout(self.timer_ping);
- start_heartbeat();
- };
-
- self.ws.onclose = function(evt) {
- console.log("Connection closed.");
- };
-
- self.ws.onerror=function(evt){
- console.log(evt);
- };
- function start_heartbeat(){
- self.timer_ping=setTimeout(function(){
- self.send("ping");
-
- self.timer_close=setTimeout(function(){
- self.ws.close();
- self.is_onlien=false;
- on_offline();
- },self.interval_close);
- },self.interval_ping);
- };
-
- function on_offline(){
-
- }
- },
- send:function(data){
- this.ws.send(data);
- },
- }
|