简体   繁体   English

如何将数据发送到使用net.createServer()连接事件创建的特定套接字?

[英]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. 对于套接字,我已经习惯了perl和POE编程,并且我正在考虑将node.js用于不是基于Web的应用程序服务器。 I have JavaScript knowledge from writing webpage user interfaces. 我具有编写网页用户界面的JavaScript知识。

I have been working with the net module and have successfully been able to connect to it from multiple clients at the same time. 我一直在使用net模块,并已成功地同时从多个客户端连接到该模块。

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. 我已经在这里和Google进行了广泛的搜索。 Lots of examples of simple tcp servers and echo servers to a single client, but nothing for multiple clients. 许多简单的tcp服务器和回显服务器到单个客户端的示例,但对于多个客户端则没有。

I am trying to do this WITHOUT socket.io as not all the clients are going to be web based. 我正在尝试在没有socket.io的情况下执行此操作,因为并非所有客户端都将基于Web。

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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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