我正在尝试设置一个node.js应用程序,该应用程序可以接收连接,并且一旦套接字结束,仍然会监听端口9001。我怎样才能做到这一点?这是我当前的代码(它在socket.end()之后没有关闭,但它不接受任何其他连接):
var net = require('net');
var mySocket;
var server = net.createServer(function(socket) {
mySocket = socket;
mySocket.on("connect", onConnect);
mySocket.on("data", onData);
});
function onConnect() {
console.log("Connected");
}
function onData(command) {
if (command == "exit") {
console.log("Exiting");
mySocket.end();
}
}
console.log("Waiting for incoming connections");
server.listen(9001);
我尝试在server.listen(9001);
之后添加另一个socket.end();
,但我收到了:Error: listen EADDRINUSE
消息。
此外,该代码是否能够同时接收来自不同地址的多个连接,并单独处理它们?
这是完整的代码。执行时,node.js从Flash应用程序接收4个命令,并且正常工作(除了onConnect()函数似乎永远不会被调用),并且“exit;”命令正确关闭套接字,但如果我重新加载Flash应用程序,它不会连接到服务器
var net = require('net');
const PACKET_SEPARATOR = 59 // ;
var connection_ack = false;
var counter = 0;
var server = net.createServer(function(socket) {
function onConnect() {
console.log("Connected to Flash");
}
function dataHandler(command) {
if (command[command.length - 1] != String.fromCharCode(PACKET_SEPARATOR) && connection_ack) {
console.log("SEP : " + PACKET_SEPARATOR + " - last : " + command[command.length - 1] + " - ack " + connection_ack);
console.log("CAUGHT EXCEPTION : WRONG PACKET FORMAT --- " + command + " --- " + command.length);
}
if (command == "exit;") {
console.log("Received exit request from " + socket.address().address + ":" + socket.address().port + " (" + socket.address().family + "). Ending connection...");
socket.end();
}
else if (command == "<policy-file-request/>\0") {
socket.write('<cross-domain-policy>\n<allow-access-from domain="*" to-ports="*" />\n</cross-domain-policy>\0', 'utf8');
console.log("Policy file sent to " + socket.address().address + ":" + socket.address().port);
player1.pxacceleration = 0;
player1.pyacceleration = 0;
connection_ack = true;
}
else {
console.log("Got data from " + socket.address().address + ":" + socket.address().port + " (" + socket.address().family + ")");
console.log("--> " + command);
counter++;
socket.write("Received " + counter + " commands;", 'utf8');
console.log("Sending : Received " + counter + " commands;");
}
}
function onData(d) {
var command = "";
for (i=0; i <= d.length - 1; i++) {
command += String.fromCharCode(d[i]);
if (d[i] == PACKET_SEPARATOR || i == d.length - 1 && !connection_ack) {
dataHandler(command);
command = "";
}
}
}
socket.on("connect", onConnect);
socket.on("data", onData);
});
console.log("Ready. Waiting for incoming connections");
server.listen(9001);
server.listen(80); //TODO : Remove?
答案 0 :(得分:1)
正如jfriend00所说,不推荐使用mySocket
作为全局。请尝试以下方法。
var server = net.createServer(function(socket) {
function onData(command) {
if (command == "exit") {
console.log("Exiting");
socket.end();
}
}
socket.on("connect", onConnect);
socket.on("data", onData);
});
...
这首先消除了对全球的需求。这也应该允许多个套接字并防止原始错误。我认为。我是新来的,所以我想我们会看到。
修改强>
好的。我一直在通过telnet与您的代码进行交互。我还阅读了一些文档。首先,应将socket.on("connect", onConnect);
侦听器(以及onConnect
函数)移动到全局范围并更改为server.on("connection", onConnect);
。这样做的原因是套接字事件侦听器connect
是客户端侦听器。我们正在服务器端。新连接的服务器端侦听器是connection
,服务器应该以与侦听特定端口上的连接相同的方式监听它。
这部分代码现在应该如下所示:
//more code up above here
....
function onData(d) {
var command = "";
for (i=0; i <= d.length - 1; i++) {
command += String.fromCharCode(d[i]);
if (d[i] == PACKET_SEPARATOR || i == d.length - 1 && !connection_ack) {
dataHandler(command);
command = "";
}
}
}
socket.on("data", onData);
});
function onConnect() {
console.log("Connected to Flash");
}
server.on("connection", onConnect);
....
//more code below here
但是,代码不会通过telnet将exit
识别为命令。我无法弄清楚这一点。既然你的问题没有涵盖这个问题,那可能只是我或你已经弄清楚了。
编辑2 下面的代码将其保留在本地。
var server = net.createServer(function(socket) {
function onConnect() {
console.log("Connected to Flash");
socket.write("we're connected");
}
....
function onData(d) {
var command = "";
for (i=0; i <= d.length - 1; i++) {
command += String.fromCharCode(d[i]);
if (d[i] == PACKET_SEPARATOR || i == d.length - 1 && !connection_ack) {
dataHandler(command);
command = "";
}
}
}
onConnect();
socket.on("data", onData);
});