We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
(function (t) { //自动重连的WebSocket function WebSocket2(url, name) { if (typeof (WebSocket) == "undefined") throw "当前浏览器不支持 WebSocket,请更换主流浏览器!"; this.name = name || "WebSocket2_"+(new Date()).getTime(); this.url = url; /* WebSocket.readyState -- this.state 0 CONNECTING 连接尚未建立 1 OPEN WebSocket的链接已经建立 2 CLOSING 连接正在关闭 3 CLOSED 连接已经关闭或不可用 */ this.state = 0; //状态 (0:未连接,1:已连接,2:关闭中,3:已关闭) this.ws = null; this.connCount = 0; this.isReConn = false; //是否正在自动重新连接 if (this.url) this.connect(); } //重连 function reconn(self) { console.log(self.name + ".reconn", "state:", self.state); if (self.state == self.OPEN) return; if (self.state == self.CLOSED) self.isReConn = false; else { self.isReConn = true; self.connect(); setTimeout(function () { reconn(self) }, 10000); } } Object.defineProperties(WebSocket2.prototype, {//writable,configurable,enumerable 这些默认为false CONNECTING: { value: 0 }, OPEN: { value: 1 }, CLOSING: { value: 2 }, CLOSED: { value: 3 } }); WebSocket2.prototype.connect = function (url) { var self = this; if (self.state == self.OPEN) return; self.url = url || self.url; if (self.ws) { self.ws.onopen = null; self.ws.onerror = null; self.ws.onclose = null; self.ws.onmessage = null; if (self.ws.readyState <= 1) self.ws.close(); self.ws = null; } self.state = self.CONNECTING; self.ws = new WebSocket(self.url); self.ws.onmessage = function (evt) { self.onmessage && self.onmessage(evt, self.url, self.name); }; self.ws.onopen = function () { self.state = self.OPEN; self.connCount++; self.isReConn = false; self.onopen && self.onopen(self.connCount, self.url, self.name); } self.ws.onerror = function (evt) { console.error(self.name + ".onerror: ", evt.type, evt); //断开连接了 if (!self.isReConn && this.readyState != 1) { self.state = self.CONNECTING; reconn(self); } self.onerror && self.onerror(evt, self.url, self.name); } self.ws.onclose = function () { console.log(self.name + ".onclose"); //主动关闭,不自动连接 //远端正常关闭,或出错导致关闭,自动重新连接 if (!self.isReConn && self.state != self.CLOSED && this.readyState != 1) { self.state = self.CONNECTING; reconn(self); } self.onclose && self.onclose(self.url, self.name); } }; WebSocket2.prototype.send = function (msg) { if (this.state != this.OPEN) throw (this.name + " WebSocket " + (this.state == this.CONNECTING ? "正在" : "尚未") + "建立连接,不能发送", msg); else this.ws.send(msg); }; WebSocket2.prototype.close = function () { if (this.ws) { self.connCount = 0; this.state = this.CLOSED; if (this.ws && this.ws.close) this.ws.close(); } }; WebSocket2.prototype.onmessage = function (evt, url, name) { console.log(this.name + ".onmessage: ", evt.data, evt.type, evt.origin); }; WebSocket2.prototype.onopen = function (n, url, name) { console.info(this.name + ".onopen:" + name + " connected " + url + " #" + n); }; WebSocket2.prototype.onclose = function (url, name) { console.info(this.name + ".onclose: closed"); }; WebSocket2.prototype.onerror = null; t.WebSocket2 = WebSocket2; })(this);
The text was updated successfully, but these errors were encountered:
No branches or pull requests
自动重连的WebSocket
The text was updated successfully, but these errors were encountered: