Reconnection of Client when server reboots in WebSocket

后端 未结 9 2239
青春惊慌失措
青春惊慌失措 2020-12-02 05:03

I am using web socket using PHP5 and the Chrome browser as client. I have taken the code from the site http://code.google.com/p/phpwebsocket/.

I run the server, and

9条回答
  •  难免孤独
    2020-12-02 05:48

    Finally, I make ws auto reconnect in vue+ts, similar as following:

    private async mounted() {
        // Connect to WebSocket
        const sn = "sb1234567890";
        const host =
            window.location.protocol == "https:"
                ? "wss://www.xxx.net"
                : process.env.DEV_TYPE === "fullstack"
                ? "ws://10.0.0.14:8528"
                : "ws://www.xxx.net:8528";
        const wsUri = host + "/feed-home/" + sn;
        await this.startWs(wsUri, sn);
        // !!!Deprecated: failed to reconnect
        // let ws = new WebSocket();
        // console.log(ws);
        // ws.onopen = async function(event) {
        //     console.log(event, "openEvent");
        //     clearInterval(that.timer);
        // };
        // ws.onclose = async function(event) {
        //     console.log(event, "close");
        //     that.timer = setInterval(() => {
        //         console.log("Heart Beat");
        //         ws.send("HeartBeat");
        //         // ws = new WebSocket("ws://10.0.0.14:8528/feed-home/" + sn);
        //         console.log(ws);
        //     }, 60000);
        // };
        // ws.onmessage = async function(event) {
        //     console.log(event, "ws");
        //     alert("get it!");
        //     await alert("please update!");
        //     await that.getHome(sn);
        // };
    }
    private wsReconnected: boolean = false; // check whether WebSocket is reconnected
    private async startWs(uri: string, sn: string) {
        let that = this;
        let ws = new WebSocket(uri);
        ws.onopen = async () => {
            if (that.wsReconnected) {
                await that.getHome(sn); // Refresh api data after reconnected
            }
            ws.send("Current client version: " + window.version);
        };
        ws.onmessage = async evt => {
            await that.getHome(sn);
            that.$message({
                type: "success",
                message: evt.data,
                showClose: true,
                center: true,
                duration: 20 * 1000
            });
        };
        ws.onclose = async () => {
            that.wsReconnected = true;
            await that.startWs(uri, sn);
            const sleep = (seconds: number) => {
                return new Promise(resolve =>
                    setTimeout(resolve, seconds * 1000)
                );
            };
            await sleep(10); // Try to reconnect in 10 seconds
            // !!!Deprecated: Use timer will cause multiply ws connections
            // if (!that.wsTimer) {
            //     // Try to reconnect in 10 seconds
            //     that.wsTimer = setInterval(async () => {
            //         console.log("reconnecting websocket...");
            //         await that.startWs(uri, sn);
            //     }, 10 * 1000);
            // }
        };
    }
    

提交回复
热议问题