1

我有一个项目是 node.js 服务器与 arduino 真正的 TCP 套接字对话。它从一个真正的 socket.io 网页接收他的所有数据,然后将其传输到 TCP 套接字。一切正常。除非:

我用新软件下载了我的 arduino,或者当我拔下以太网电缆时,当我在 arduino 上打开串行监视器两次时。我得到了 events.js:85 throw er; // 未处理的“错误”事件错误:在 TCP.onread (net.js:550:26) 处读取 ECONNRESET 在 exports._errnoException (util.js:746:11)

有谁知道是什么问题。我该如何解决它,尤其是自动连接功能。

var express = require('express');
var app     = express();
var hbs     = require('hbs');
var port    = 3000;
var http = require('http').Server(app);
var io = require('socket.io')(http);
net = require('net');
var tcpGuests = [];

app.set('view engine', 'html');
app.engine('html', hbs.__express);
app.use(express.bodyParser());
app.use(express.static('public'));


app.get('/', function(req, res) {
    res.render('index', {title:"HTML5-SuperTemplate"});
});

app.get('/about', function(req, res) {
    res.render('about', {title:"About Me"});
});

app.get('/instructions', function(req, res) {
    res.render('instructions', {title:"Instructions"});
});

app.get('/buttons', function(req, res) {
    res.render('buttons', {title:"Buttons"});
});

app.get('/Lists', function(req, res) {
    res.render('Lists', {title:"Lists"});
});

app.get('/menus', function(req, res) {
    res.render('menus', {title:"Menus"});
});

app.get('/tables', function(req, res) {
    res.render('tables', {title:"Tables"});
});

app.get('/tooltips', function(req, res) {
    res.render('tooltips', {title:"Tooltips"});
});

app.get('/typography', function(req, res) {
    res.render('typography', {title:"Typography"});
});

app.get('/hr', function(req, res) {
    res.render('hr', {title:"Horizontal Rules"});
});

app.get('/icons', function(req, res) {
    res.render('icons', {title:"ICONS"});
});

app.get('/code', function(req, res) {
    res.render('code', {title:"CODE & PRE"});
});

app.get('/tabs', function(req, res) {
    res.render('tabs', {title:"TABS"});
});

app.get('/breadcrumbs', function(req, res) {
    res.render('breadcrumbs', {title:"Breadcrumbs"});
});

app.get('/grids', function(req, res) {
    res.render('grids', {title:"Grid System"});
});

app.get('/images', function(req, res) {
    res.render('images', {title:"IMAGES"});
});

app.get('/slideshow', function(req, res) {
    res.render('slideshow', {title:"Slideshow"});
});

app.get('/forms', function(req, res) {
    res.render('forms', {title:"Forms"});
});

app.get('/classes', function(req, res) {
    res.render('classes', {title:"Classes"});
});

io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
    console.log('received on io-socket:'+msg);
  });
});

http.listen(3000, function(){
  console.log('listening on *:3000');
});

// socket.io, I choose you
io.on('connection', function(socket){
  socket.on('chat message', function(msg){
    io.emit('chat message', msg);
    console.log('received on io-socket:'+msg);
    for (g in tcpGuests) {
        tcpGuests[g].write(msg);
    }
  });
});

//tcp socket server
var tcpServer = net.createServer(function (socket) {
  console.log('tcp server running on port 1337');
  console.log('web server running on http://localhost:3000');
});

tcpServer.on('connection',function(socket){
    socket.write('connected to the tcp server\r\n');
    console.log('num of connections on port 1337: ' + tcpServer.connections);

    tcpGuests.push(socket);


    socket.on('data',function(data){
        console.log('received on tcp socket:'+data);
        socket.write(' msg received\r\n');


        //send data to guest socket.io chat server
        for (g in io.clients) {
            var client = io.clients[g];
            client.send({message:["arduino",data.toString('ascii',0,data.length)]});
          }
    })
});
tcpServer.listen(1337);
4

1 回答 1

0

您不进行错误处理。每次出现连接错误(意外的电缆拔出)时,node.js 都会抛出您收到的错误消息。您可以捕获这些错误,以便应用程序不会退出。

添加一些错误处理程序。tcpServer.on('error', ...), socket.on('error', ...), io.on('error', ...), 等等。也许还有其他一些错误处理程序。

于 2015-03-18T00:26:22.037 回答