1

尝试将数据从串行设备发送到 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 新手,这可能不是最好的方法。任何建议,将不胜感激。

4

1 回答 1

0

好的,我想我在 dataStream.on('data' ... 我添加了一个 setTimeout

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

如果数据停止进入,超时就会执行,因为每次数据进入时都会重复清除它。doReconnect 函数不断尝试连接并向客户端发送一条消息,说明正在发生错误。

于 2013-01-12T21:54:05.123 回答