gpt4 book ai didi

node.js - 如何让 Node.js 应用程序在关闭套接字后继续监听?

转载 作者:太空宇宙 更新时间:2023-11-04 01:00:43 25 4
gpt4 key购买 nike

我正在尝试设置一个 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);

我尝试在 socket.end(); 之后添加另一个 server.listen(9001);,但收到一条: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?

最佳答案

正如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

但是,代码无法将 exit 识别为通过 telnet 的命令。我无法弄清楚这一点。由于您的问题没有涵盖这个问题,因此可能只是我或您已经解决了。

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

关于node.js - 如何让 Node.js 应用程序在关闭套接字后继续监听?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26963317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com