gpt4 book ai didi

Node.js 聊天应用程序无法加载

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

我刚刚创建了一个 node.js 聊天应用程序..但该应用程序无法加载。该代码是非常基本的 Node.js 聊天应用程序:

// Load the TCP Library
var net = require('net');

// Keep track of the chat clients
var clients = [];

// Start a TCP Server
net.createServer(function (client) {

console.log("Connected!");

clients.push(client);

client.on('data', function (data) {
clients.forEach(function (client) {
client.write(data);
});
});

}).listen(5000);

// Put a friendly message on the terminal of the server.
console.log("Chat server is running\n");

编译后,我在 chrome 浏览器中编写 localhost:5000 但页面一直在加载并且永远不会完成。

但是,以下代码可以完美运行:

// Load the TCP Library
net = require('net');

// Start a TCP Server
net.createServer(function (client) {
client.write("Hello World");
client.end();
}).listen(5000);


我在计算机上运行 Windows 7 64 位,并且使用 Chrome。

提前致谢!

最佳答案

您正在使用 net 模块创建 TCP/IP 服务器,但您正在使用 Web 浏览器使用 http 协议(protocol)访问它。

这彼此不匹配。

尝试使用 telnet 连接到您的服务器,一切都应该没问题。

或者,如果您希望能够使用网络浏览器进行连接,则需要使用 http 模块而不是 net 模块。

关于Node.js 聊天应用程序无法加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14841369/

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