var apexwebsocket ={ ws:null, timeoutid:null, timeout: 15000, //心跳发送间隔时间 severtimeout: 20000, //服务端超时时间 timeoutobj: null, servertimeoutobj: null, "ws_core" :"market",//行情类型 /** * 客户端是否支持websocket * @returns {boolean} */ issupport:function(){ var b = false; if ('websocket' in window) { b = true; } else if ('mozwebsocket' in window) { b = true; } return b; }, /** * 获取ws链接地址 * @returns {string} */ gettargeturl:function (topic) { if(window["websocket_host"] != undefined){ return window["websocket_host"] + "/websocket/bidinfoserver/" + apexwebsocket["ws_core"] + "/" + topic; } var target = window["p_basepath"] + "/websocket/bidinfoserver/" + apexwebsocket["ws_core"] + "/" + topic; //ws url上增加引用地址pathname,有助于日志分析,问题排查 if(target.indexof("?") != -1){ target = target + "&ref="+location.pathname+"&token="+encodeuricomponent(window["token"]); }else{ target = target + "?ref="+location.pathname+"&token="+encodeuricomponent(window["token"]); } if (window.location.protocol == 'http:') { return 'ws://' + window.location.host + target; } else { return 'wss://' + window.location.host + target; } }, connect : function (topic, messagecallback, closecallback){ if(apexwebsocket.timeoutid != null){ base.logger.info('websocket 链接...cleartimeout'); cleartimeout(apexwebsocket.timeoutid); } base.logger.info('websocket 链接延迟创建...'); //fix:通过timeout方式解决页面重新加载导致的重连问题? todo apexwebsocket.timeoutid = settimeout(function () { apexwebsocket.connecttimeout(topic, messagecallback, closecallback); },3000); }, connecttimeout : function (topic, messagecallback, closecallback) { var _this = this; var target = apexwebsocket.gettargeturl(topic); if ('websocket' in window) { apexwebsocket.ws = new websocket(target); } else if ('mozwebsocket' in window) { apexwebsocket.ws = new mozwebsocket(target); } else { alert('您的浏览器不支持websocket.'); return; } apexwebsocket.ws.onopen = function () { _this.heartcheck(); base.logger.info('websocket 链接已创建.'); }; apexwebsocket.ws.onmessage = function (event) { //log('received: ' + event.data); if (event.data === 'heartback'){ //心跳返回消息 _this.heartcheck(); return; } if(typeof messagecallback == 'function'){ try{ var json = $.parsejson(event.data); if(json.success){ messagecallback(json.object); } }catch (e){ base.logger.error(e); } } }; apexwebsocket.ws.onclose = function (event) { base.logger.info('websocket 链接关闭, code: ' + event.code + (event.reason == "" ? "" : ", reason: " + event.reason)); //if(!!!apexwebsocket.onclose){ // apexwebsocket.onclose = true; // base.logger.info("websocket 链接关闭,不进行回调操作。"); // return false;//手动关闭 //} if(typeof messagecallback == 'function'){ base.logger.info('websocket 进行回调 '); settimeout(function () { closecallback(event); },500); } }; }, isclosed:function(){ /** * readystate 说明: * 0 connecting 连接尚未建立 * 1 open websocket的链接已经建立 * 2 closing 连接正在关闭 * 3 closed 连接已经关闭或不可用 */ return apexwebsocket.ws == null || apexwebsocket.ws.readystate == 3; }, close:function(triggeronclose){ apexwebsocket.onclose = triggeronclose; if(!apexwebsocket.isclosed()){ return apexwebsocket.ws.close(); } }, /** * 心跳检查 * 间隔一定时间发送心跳包 * 在超时时间内无响应 断开主动重连 */ heartcheck: function(){ var _this = this; this.timeoutobj && cleartimeout(this.timeoutobj); //timeoutobj 不为空清除timeoutobj定时器 this.servertimeoutobj && cleartimeout(this.servertimeoutobj); //servertimeoutobj 不为空 清除servertimeoutobj定时器 this.timeoutobj = settimeout(function(){ //这里发送一个心跳,后端收到后,返回一个心跳消息, //onmessage拿到返回的心跳就说明连接正常 _this.ws.send("heartbeat"); // 心跳包 base.logger.info('发送心跳包'); //计算答复的超时时间 _this.servertimeoutobj = settimeout(function() { base.logger.info('后端无响应 断开重连'); _this.ws.close(); }, _this.severtimeout); }, this.timeout) }, }