将实时数据发送到webclients错误捕获

时间:2013-01-11 01:30:05

标签: node.js socket.io

尝试将数据从串行设备发送到Web客户端。我使用串行到网络代理,ser2Net使数据可用于作用于数据的服务器,并将操作版本的数据发送到Web客户端。客户端指定ser2net主机和端口的位置。此操作的核心在node.js中编码,如下所示:

function getDataStream(socket, dataSourcePort, host) {
    var dataStream = net.createConnection(dataSourcePort, host),
        dataLine = "";

    dataStream.on('error', function(error){
        socket.emit('error',{message:"Source not found on host:"+ host + " port:"+dataSourcePort});
        console.log(error);
    });

    dataStream.on('connect', function(){
        socket.emit('connected',{message:"Data Source Found"});
    });

    dataStream.on('close', function(){
        console.log("Close socket");     
    });

    dataStream.on('end',function(){
        console.log('socket ended');
        dataConnection.emit('lost',{connectInfo:{host:host,port:dataSourcePort}});
    });

    dataStream.on('data', function(data) {

        // Collect a line from the host
        line += data.toString();
        // Split collected data by delimiter
        line.split(delimiter).forEach(function (part, i, array) {
        if (i !== array.length-1) { // Fully delimited line.
            //push on to buffer and emit when bufferSendCommand is present
            dataLine = part.trim();
            buffer.push(part.trim());
            if(part.substring(0, bufferSendCommand.length) == bufferSendCommand){
                gotALine.emit('new', buffer);
                buffer=[];
            }
        }
        else {
          // Last split part might be partial. We can't announce it just yet.
          line = part;
        }
      });
    });
    return dataStream; 
}


io.sockets.on('connection', function(socket){

    var stream = getDataStream(socket, dataSourcePort, host);

    //dispense incoming data from data server
    gotALine.on('new', function(buffer){
        socket.emit('feed', {feedLines: buffer});
    });

    dataConnection.on('lost', function(connectInfo){
        setTimeout(function(){
            console.log("Trying --- to reconnect ");
            stream = getDataStream(socket, connectInfo.port, connectInfo.host);
        },5000);
    });

    // Handle Client request to change stream 
    socket.on('message',function(data) {
        var clientMessage = JSON.parse(data);
        if('connectString' in clientMessage
            && clientMessage.connectString.dataHost !== ''
            && clientMessage.connectString.dataPort !== '') {
            stream.destroy();
            stream = getDataStream(socket,
                clientMessage.connectString.dataPort,
                clientMessage.connectString.dataHost);
        }
    });    
});

这很有效,直到串行设备关闭并且ser2net停止发送数据。我试图抓住套接字并重新连接的尝试无法正常工作。事件正确发出但setTimeout只发送一次。我想找到一种方法,在向客户端发送消息通知或重试尝试时继续尝试重新连接。我是node.js新手,这可能不是最好的方法。任何建议将不胜感激。

1 个答案:

答案 0 :(得分:0)

好的我想我在dataStream.on中找到了它('data'......我添加了一个setTimeout

clearTimeout(connectionMonitor);
connectionMonitor = setTimeout(function(){doReconnect(socket);}, someThresholdTime);

如果数据停止进入,则会执行超时,因为每次数据进入时都会重复清除.DoReconnect函数会继续尝试连接并向客户端发送一条消息,说明正在发生的事情。