I'm trying to recreate the functionality of a hardware serial server with Node and it's actually working, but I'm getting errors from socket instances that have been closed.

我正在尝试使用Node重新创建硬件串行服务器的功能,它实际上正在工作,但我从已关闭的套接字实例中收到错误。

Here's a simplified version of the app to show what I'm doing...

这是应用程序的简化版本,用于显示我正在做的事情......

var net = require('net');
var SerialPort = require('serialport');
var connectionCounter = 0;
var port = new SerialPort('/dev/ttyUSB0', function () {
    var server = net.createServer();
    server.on('connection',function(socket) {
      connectionCounter++;
      var connNumber = connectionCounter;
      socket.on('error', function () {
        console.log('socket ' + connNumber + ' errored');
      });
      socket.on('data', function(data) {
        port.write(data);
      });
      port.on('data', function(data) {
        socket.write(data);
      });
    });
    server.listen(8887, '127.0.0.1');
  }
});

So the first chunk of code that's sent into the 8887 port works fine, and it returns the data back out through the socket. The errors start on the second chunk. In the example, I'm keeping a count of the socket instances and outputting the socket instance number with the error. So as the program runs, the number of sockets instances keeps going up. The most recent instance will eventually handle the data, but I can't figure out what I need to delete to clean up all of the previous socket instances so they'll stop trying to process the incoming data.

因此,发送到8887端口的第一个代码块工作正常,它通过套接字返回数据。错误从第二个块开始。在示例中,我将保留套接字实例的计数并输出带有错误的套接字实例编号。因此,当程序运行时,套接字实例的数量不断增加。最近的实例最终将处理数据,但我无法弄清楚我需要删除什么来清理所有以前的套接字实例,因此他们将停止尝试处理传入的数据。

I've tried socket.end() and socket.destroy(), but those don't seem to work . Do I need to go as far as deleting the server itself and recreating it?

我已经尝试过socket.end()和socket.destroy(),但那些似乎不起作用。我是否需要删除服务器本身并重新创建它?

2 个解决方案

#1


0

you can use array for storing sockets later on you can delete. this is sample code hope you got the idea

您可以在以后使用数组存储套接字,也可以删除。这是示例代码,希望您有这个想法

var net = require('net');
var SerialPort = require('serialport');
var connectionCounter = 0;
var mySockets = [];
var port = new SerialPort('/dev/ttyUSB0', function () {
    var server = net.createServer();
    server.on('connection',function(socket) {
    mySockets.push(socket);
      connectionCounter++;
      var connNumber = connectionCounter;
      socket.on('error', function () {
        console.log('socket ' + connNumber + ' errored');
      });
      socket.on('data', function(data) {
        port.write(data);
      });
      port.on('data', function(data) {
        socket.write(data);
      });
    });
    server.listen(8887, '127.0.0.1');
  }
//get the sockets you want to delete
var s = mySockets.pop();
s = null;
});

更多相关文章

  1. 如何避免pro拖拉机中的“jasmin .suite() required”错误消息?
  2. 常用验证JS代码基础及实例
  3. “错误:路径必须是字符串”(v5.10.0)
  4. javascript小实例,PC网页里的拖拽
  5. 使用bootstrap模式框的自定义选择框错误
  6. 获取Backbone Model实例的模型/类名
  7. $.each遍历JSON字符串和 Uncaught TypeError: Cannot use 'in' o
  8. 2_python连接MariaDB错误
  9. 获取错误“ValueError:int()的无效文字,基数为10:'3128;'在运行Tensor

随机推荐

  1. Professional Android 2 Development - 8
  2. Android 布局各个属性的含义
  3. Android面试题整理(中)
  4. TextView之二:常用属性
  5. android 学习开始
  6. Android TextView跑马灯不动 及属性
  7. android字体的工作原理
  8. Android 图表开源框架之MPAndroidChart L
  9. android一些操作
  10. Android特效 - 收藏集 - 掘金