简体   繁体   中英

How to send data to specific sockets created using net.createServer() connection event?

I am used to perl and POE programming when it comes to sockets, and I am looking at using node.js for an application server that is not web based. I have JavaScript knowledge from writing webpage user interfaces.

I have been working with the net module and have successfully been able to connect to it from multiple clients at the same time.

var net = require('net');
var port = 63146;
var socketNum = 0;

var adminServer = net.createServer(function(socket){
     //add connection count
     socketNum++;
     socket.nickname = "Con# " + socketNum;
     var clientName = socket.nickname;

     console.log(clientName + "connected from " + socket.remoteAddress);
     socket.write("You have been given the client name of " + clientName);
 });

adminServer.listen(port, function() {
    console.log("Server listening at" + port);
});

So the issue I am having is if I have another function created that needs to send data to a specific client, not a broadcast to all of them, I can't figure out how to do it.

I have done extensive searching here and Google. Lots of examples of simple tcp servers and echo servers to a single client, but nothing for multiple clients.

I am trying to do this WITHOUT socket.io as not all the clients are going to be web based.

Any help would be appreciated,

Z

You have to store them yourself somehow, whether that's simply adding to an array or adding to an object keyed on some unique identifier for example.

Here's using an object:

var net = require('net');
var port = 63146;
var socketNum = 0;
var sockets = Object.create(null);

var adminServer = net.createServer(function(socket){
     //add connection count
     socketNum++;
     socket.nickname = "Con# " + socketNum;
     var clientName = socket.nickname;

     sockets[clientName] = socket;
     socket.on('close', function() {
       delete sockets[clientName];
     });

     console.log(clientName + " connected from " + socket.remoteAddress);
     socket.write("You have been given the client name of " + clientName);
 });

adminServer.listen(port, function() {
    console.log("Server listening at" + port);
});

Then you can find a particular socket by its assigned nickname.

Here is the example working code. Hopefully this will be useful to others!

var net = require('net');
var port = 63146;
var conSeen = Object.create(null);
var socketNum = 0;

var adminServer = net.createServer(function(socket){
     //add connection count
     socketNum++;
     socket.nickname = "Con" + socketNum;
     var clientName = socket.nickname;
     //console.log(socket);

 conSeen[clientName] = socket;

 socket.on('close', function() {
   delete sockets[clientName];
 });

 console.log(clientName + " has connected from " + socket.remoteAddress);
 socket.write("You have been given the client name of " + clientName + "\r\n");
 socket.on('data', function(inputSeen) {
          var clientName = socket.nickname;
          var input = inputSeen.toString('utf8'); 
          input = input.replace(/(\r\n|\n|\r)/gm,"");
          console.log("Saw : " + input + " from " + clientName + "\r\n");
          if (input === 'sendTest') {
            conSeen[clientName].write('test 123\r\n');
          }
     });

});



adminServer.listen(port, function() {
    console.log("Server listening on " + port);
});

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