var socket, limitCount = 5; if (!window.WebSocket) { window.WebSocket = window.MozWebSocket; } if (window.WebSocket) { socket = new WebSocket("ws://192.168.3.96:8088/glwork/onlinewebsocket/" + $.cookie("GlWorkPlatform-userid")); socket.onmessage = function (event) { $("#onlineUsers").html(event.data + "人"); $.cookie("GlWorkPlatform-onlineUsers", event.data); console.log(event) }; socket.onopen = function (event) { console.log(event) }; socket.onclose = function (event) { console.log(event) if ($.cookie("GlWorkPlatform-onlineUsers") != null) { reconnet() //socket = new WebSocket("ws://192.168.3.96:8088/glwork/onlinewebsocket/" + $.cookie("GlWorkPlatform-userid")); } else { console.log("服务器已断开连接!") } }; setInterval(function () { socket.send("") }, 1000 * 55) } else { sp.dialog("抱歉,您的浏览器不支持WebSocket协议!"); } function reconnet(){ if (limitCount > 0) { console.log("第" + (6 - limitCount) + "次重连!"); limitCount-- socket = new WebSocket("ws://192.168.3.96:8088/glwork/onlinewebsocket/" + $.cookie("GlWorkPlatform-userid")); } else { console.log("重连失败,请重新登录!") } }