websocket 断线重连
1 var ws = new WebSocket("ws://127.0.0.1:8282"); 2 console.log(ws); 3 ws.onopen = function(e){ 4 console.log('链接成功了'); 5 }; 6 ws.onmessage = function(e){ 7 // console.log(e); 8 var msg = JSON.parse(e.data); 9 // console.log(e.data); 10 switch (msg.type) { 11 case 'release': 12 relinit(true); 13 break; 14 default: 15 console.log(e.data); 16 } 17 18 } 19 ws.onclose = function(e){ 20 alert('链接断开了'); 21 console.log(this.readyState); 22 this.close(); 23 reconect(); 24 25 } 26 function reconect() { 27 console.log('再次链接'); 28 ws = new WebSocket("ws://127.0.0.1:8282"); 29 ws.onopen = function (e) { 30 console.log('再次链接成功'); 31 // clearTimeout(xxoo); 32 }; 33 ws.onmessage = function(e){ 34 // console.log(e); 35 var msg = JSON.parse(e.data); 36 // console.log(e.data); 37 switch (msg.type) { 38 case 'release': 39 relinit(true); 40 break; 41 default: 42 console.log(e.data); 43 } 44 45 }; 46 ws.onclose = function(e){ 47 console.log('链接断开了'); 48 this.close(); 49 // clearTimeout(xxoo); 50 console.log(this.readyState); 51 52 reconect(); 53 54 };