分享个websocket插件带断线重连

分享 未结
15 1806
用个名字真难
悬赏:20飞吻
layui.define(["jquery"],function(a){"use strict";var c,b=layui.jquery,d=function(){},e=function(){try{var a=new WebSocket(d.config.wsUrl);return a.onmessage=function(a){d.config.onmessage(a),g.reset()},a.onclose=function(){d.config.onclose(),f()},a.onerror=function(){d.config.onerror(),f()},a.onopen=function(){d.config.onopen(),g.start()},a}catch(b){f()}},f=function(){return d.config.lockReconnect?void 0:(d.config.lockReconnect=!0,1==d.config.ShInt?!1:(setTimeout(function(){d.config.isqidong=!1,console.log("服务器重新连接中...."),layui.socket.init(d.config),d.config.lockReconnect=!1},2e3),void 0))},g={timeout:d.config.timeout,timeoutObj:null,serverTimeoutObj:null,reset:function(){d.config.isqidong=!0,console.log("心跳"),clearTimeout(this.timeoutObj),clearTimeout(this.serverTimeoutObj),g.start()},start:function(){var a=this;this.timeoutObj=setTimeout(function(){d.config.isqidong=!0,1==c.readyState&&c.send(d.config.HeartBeat),a.serverTimeoutObj=setTimeout(function(){d.config.isqidong=!1,c.close()},a.timeout)},this.timeout)}};d.prototype.heartCheck={start:function(){g.start()},reset:function(){g.reset()}},d.prototype.config={wsUrl:"",ShInt:"0",lockReconnect:!1,HeartBeat:'{"type":"HeartBeat"}',onopen:function(){console.log("服务器已连接")},onmessage:function(){console.log("收到消息....")},onerror:function(){console.log("服务错误")},onclose:function(){console.log("服务已关闭")}},d.prototype.init=function(a){var f=this;return d.config=b.extend({},f.config,d.config,a),c=e()},d.prototype.close=function(){clearTimeout(g.timeoutObj),clearTimeout(g.serverTimeoutObj),d.config.ShInt=1,c.close()},d.prototype.send=function(a){c.send(a)},a("socket",new d)});
layui.use(['socket'], function(){

var socket = layui.socket;

socket.init({
wsUrl: 'ws://服务器地址'
,HeartBeat:'{"type":"HeartBeat"}'//心跳数据
,timeout:60000//60秒发送心跳包
,onopen:function(){
//握手成功
}
,onmessage:function(event){
var data = JSON.parse(event.data);
//收到信息
}
});

//socket.send(JSON.stringify(json));//发送

// socket.close()//断开
});
回帖