首页 文章

JS WebRTC在计时器中发送数据在结束前重新加载时崩溃

提问于
浏览
0

我在节点服务的server.js javascript文件中有这段简单的代码:

function multiStep(myConnection, data) {
    var i=0;
        var myTimer=setInterval(function() {
        if (i<data.length){
            var element=JSON.stringify(data[i]);
            console.log("mando elemento: "+element);
            myConnection.send(element);
            i++;
        }
    }, 3000);
}

//require our websocket library 
clearInterval(myTimer);
var WebSocketServer = require('ws').Server; 
//creating a websocket server at port 9090 
var wss = new WebSocketServer({port: 9090}); 

//when a user connects to our sever 
wss.on('connection', function(connection) {

    loadJSON(function(response) { 

//when server gets a message from a connected user 
        connection.on('message', function(message){ 
                console.log("Got message from a user:", message); 
        });
        var json = JSON.parse(response);
        multiStep(connection, json, 0);
    })
});

loadJSON只是从另一个网站加载一个json数据文件 . 当我第一次运行客户端应用程序或超时结束时一切正常 . 然而,如果我在超时未完成时重新加载页面,当我尝试使用服务器上的旧页面与报告的连接时,我会崩溃:

/var/www/html/MATERIALI/phonegap/node_modules/ws/lib/WebSocket.js:219否则抛出新错误('未打开'); ^错误:未在WebSocket.send(/var/www/html/MATERIALI/phonegap/node_modules/ws/lib/WebSocket.js:219:16)处于null处打开 . (/var/www/html/MATERIALI/phonegap/WebRTC/server.js:36:9)在包装器[as _onTimeout](timers.js:261:14)处于Timer.listOnTimeout [as onoutout](timers.js: 112:15)

事实上,在重新加载页面的情况下,我可以简单地忽略旧会话 . 在这些情况下如何避免服务器崩溃?

2 回答

  • 0

    好的,我想我找到了解决方案;函数multiStep变为:

    function multiStep(myConnection, data) {
         var i=0;
         clearInterval(myTimer);
         myTimer=setInterval(function() {
              if (i<data.length){
                   var element=JSON.stringify(data[i]);
                   console.log("mando elemento: "+element);
                   try {
                        myConnection.send(element);
                        console.log("mandato elemento");
                   } catch(err) {
                        console.log('Websocket error: %s', err);
              }
              i++;
           } else {
           }
        }, 3000);
    }
    

    并且它不再崩溃 .

  • 1

    你需要做一些检查 . 您的代码假设一切都是100% .

    var json = JSON.parse(response);
    multiStep(connection, json, 0);
    
    • 您假设响应中有数据(可能为空,或包含非json数据)

    • 在将json传递给multiStep之前,还应检查json是否为有效数组

    • 函数multiStep还假定 data.length 将返回一些数字

    这可能不是完整的答案,但它应该让您开始使代码更加健壮 .

    它可能在 myConnection.send(element); 失败了,但这可能只是你在途中缺乏检查的一个症状(你也可以在发送东西之前检查myConnection是否仍然有效)

    参考https://developer.mozilla.org/en-US/docs/Web/API/WebSocket,您应该能够检查 myConnection.readyState 值:

    Ready state constants

    readyState属性使用这些常量来描述WebSocket连接的状态 .

    Constant    Value   Description
    CONNECTING  0   The connection is not yet open.
    OPEN    1   The connection is open and ready to communicate.
    CLOSING 2   The connection is in the process of closing.
    CLOSED  3   The connection is closed or couldn't be opened.
    

    您的代码现在看起来像这样:

    console.log("mando elemento: "+element);
        if (myConnection.readyState === 1)
                myConnection.send(element);
        else 
                console.log("web socket not open");
    

相关问题