Skip to content
New issue

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

自动重连的WebSocket #19

Open
lzpong opened this issue Dec 7, 2020 · 0 comments
Open

自动重连的WebSocket #19

lzpong opened this issue Dec 7, 2020 · 0 comments

Comments

@lzpong
Copy link
Owner

lzpong commented Dec 7, 2020

自动重连的WebSocket

(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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant