简体   繁体   中英

nodejs tcp listener script

I have the following situation and just wanted to check if I am doing it right. I have a couple of devices at my customers (RS232). Now I have a RS232-WIFI dongle connected with it, so data out of the device is sent over to my server (it outputs a datastring, example: {12,1,etc ). On my server I have a NodeJS script running, that opens up a port and fetches all data coming in.

var net = require('net');
var host = '1.1.1.1';
var servers = [];
var ports = [20000, 20001, 20002, 20003, 20004];

// Create servers
ports.forEach(function (port) {

var s = net.createServer(function (sock) {
    // We have a connection - a socket object is assigned to the connection automatically
    console.log('CONNECTED (' + sock.localPort + '): ' + sock.remoteAddress + ':' + sock.remotePort);

    // Add a 'data' event handler to this instance of socket
    sock.on('data', function (data) {
        // post data to a server so it can be saved and stuff
        postData(data.toString(), sock);

        // close connection
        sock.end();
    });

    sock.on('error', function (error) {
        console.log('******* ERROR ' + error + ' *******');

        // close connection
        sock.end();
    });
});

s.listen(port, host, function () {
    console.log('Server listening on ' + host + ':' + s.address().port);
});

servers.push(s);
});

Okay, so this works pretty good. But I am wondering, sometimes not all of the data is posted at once, sometimes I get {12, and after a second I get the rest (or even more times is needed). What can I do to optimize this script further? Do I need to call sock.end(); after receiving data? Does this hurt network performance for me or my customers? If you guys need more info let me know.

That depends on the protocol of your devices, if the devices use each connection for a chunk of data, you can write the program like so: (Do not close the socket on data event)

....
// socket will close and destroy automatically after the device close the connection
var s = net.createServer(function (sock) {
    sock.setEncoding('utf8');

    var body = "";
    sock.on('data', function (data) {
        body = body + data;
    });

    sock.on('end', function() {
        console.log(data);
        postData(data);
    });

    // TODO error handling here
});
....

Note: Socket is not guaranteed to give you all data at once, you should listen data event then concat all chunks before using.

If your devices don't close socket, you will not receive on('end') , then the code should be like this:

....
var s = net.createServer(function (sock) {
    sock.setEncoding('utf8');

    // var body = "";
    sock.on('data', function (data) {
        // body = body + data;
        postData(data);
    });

    sock.on('end', function() {
        console.log('end');
    });

    // TODO error handling here
});
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM