页面刷新后Websocket重新连接

时间:2019-03-14 21:13:55

标签: node.js websocket

我正在将websocket与HTTP服务器一起用于Node.js SPA。我使用websocket的目的是从服务器读取索引文件并将其推送到客户端。 Websocket正常工作,但刷新页面时除外。

有人可以协助我重新连接网络套接字吗? 我正在监听onClose事件,并调用创建websocket的方法。刷新页面后出现的错误是:

Socket encountered error:  write after end Closing socket

/home/pi/data_collector_access_point/node_modules/ws/lib/websocket.js:829
websocket.readyState = WebSocket.CLOSING;
                       ^

TypeError: Cannot set property 'readyState' of undefined
    at Socket.socketOnClose (/home/pi/data_collector_access_point/node_modules/ws/lib/websocket.js:829:24)
    at emitOne (events.js:121:20)
    at Socket.emit (events.js:211:7)
    at TCP._handle.close [as _onclose] (net.js:567:12)

Server.js

serverWS(address){
        let server = address
        const WebSocket = require('ws');
        let wss = new WebSocket.Server({ server });
        var socketstate = 1;
        wss.on('connection', function connection(ws, req) {

            ws.on('message', function incoming(message) {
            });

            ws.onclose = function(){
                socketstate = 0
                console.log('ws.onclose()')
                setTimeout(function(){
                    console.log('reconnct ', server)
                    let fti_server = new create_server()
                    fti_server.serverWS(server)
                },3000);
            };

             ws.onerror = function(err) {
                console.error('Socket encountered error: ', err.message, 'Closing socket');
                ws.close();
             };

            function sendme(data){
                switch(socketstate){
                    case 0:
                        console.log('sendme() socket state', socketstate)
                        break;
                    case 1:
                        console.log('sendme() socket state', socketstate)
                        if(ws.readyState == 1){
                            updater((data)=>{
                                ws.send(data)   
                            })
                        }
                        break;
                }


            }

            function reOpen(){
                console.log('reopen', wss)
                wss = new WebSocket.Server({ server });
            }

            setInterval(()=>{
                switch(ws.readyState){
                    case 1 :
                        console.log('switch case ws state on')
                            sendme(data)
                        break;
                    case 2 :
                        console.log('ws state 2')
                        break
                    case 3 :
                        console.log('switch case ws state not on')
                        break;

                }
            },1000); 
        })

    }

我正在监听websocketReadystate来查看websocket是否处于打开,关闭,关闭等状态。一旦打开,我将继续发送索引数据。

我正在引用此reconnection situation

1 个答案:

答案 0 :(得分:0)

对于那些感兴趣的人,我可以通过允许浏览器建立连接而无需再次调用websocket设置功能来使它起作用。我修改了onclose(),它可以正常工作!

ws.onclose = function(){
                socketstate = 0
                ws.close()
            };