gpt4 book ai didi

javascript - 创建简单的 Node js 服务器和客户端

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

我想要做的是拥有一台服务器,一个客户端将发送更新,而多个其他客户端将接收这些更新并更新页面的相关部分。

使用 Node js 一定很容易做到这一点,但我只是不知道从哪里开始。

这里有没有人可以帮助我并告诉我如何启动这个客户端和服务器。

非常感谢!

我到处寻找可以帮助我的东西,但最终都失败了......

<小时/>

更新

我想使用socket.io和nodeJS来建立连接。

我有我上网的服务器的起始代码:

var http = require('http'),
io = require('socket.io'), // for npm, otherwise use require('./path/to/socket.io')

server = http.createServer(function(req, res){
// your normal server code
res.writeHead(200, {'Content-Type': 'text/html'});
res.end('<h1>Hello world</h1>');
});
server.listen(5454);

// socket.io
var socket = io.listen(server);
socket.on('connection', function(client){
// new client is here!
console.log('new connection!');
client.on('message', function(){ console.log('send message') })
client.on('disconnect', function(){ console.log('disconnect') })
});

我还有客户端的代码。但它有错误:

  <script src="http://cdn.socket.io/stable/socket.io.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script>
var socket = new io.Socket('localhost');
socket.connect();
console.log(socket);
socket.on('connect', function(evt){ console.log('connected',evt) })
socket.on('message', function(evt){ console.log('got message',evt) })
socket.on('disconnect', function(evt){ console.log('disconnected',evt) })
</script>

更新2:

这就是我运行服务器时发生的情况:

[nlubin@localhost websocket]$ sudo node server.js
10 Mar 17:40:49 - socket.io ready - accepting connections

好的,这是我运行客户端时在 chrome 控制台中看到的内容:

    Unexpected response code: 301
1299796919120Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919120".
1299796919120Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919120".
1299796919130Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919130".
1299796919130Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919130".
1299796919151Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919151".
1299796919157Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919157".
1299796919162Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919162".
1299796919166Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919166".
1299796919170Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919170".
1299796919174Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919174".
1299796919177Failed to load resource: the server responded with a status of 404 (Not Found)
XHR finished loading: "http://localhost/socket.io/xhr-polling//1299796919177".
1299796919181Failed to load resource: the server responded with a status of 404 (Not Found)

那些 XHR 错误不断发生

最佳答案

如果您想得到一个像样的答案,您确实需要向我们提供更多有关您正在做的事情的背景信息。

我假设当您说“客户端”时您指的是网络浏览器。如果我理解正确的话,您将希望将数据从服务器“推送”到多个网络浏览器客户端。在这种情况下,您需要在浏览器和服务器之间保持持久连接打开 - 请查看 Socket.io .

基本流程是(1)用户访问您的网页,(2)该网页上的一些 JavaScript 将连接到您的 Node.js/Socket.io 服务器,(3)服务器然后可以将消息推送到任何/所有当前连接的浏览器,并且浏览器可以将消息发送到服务器,(4)对于来自服务器的消息,每个客户端上运行的 Javascript 可以解释这些消息并对网页的 DOM 进行适当的更改。

<小时/>

编辑:您的客户端 JavaScript 无法连接到服务器。如果它们在同一台计算机上运行(localhost),那么这可能意味着端口有问题。

尝试为您的客户端指定“端口”选项,在您的例子中为“5454”。

var socket = new io.Socket('localhost',{'port':5454});

请务必重新启动 Node.js 服务器并刷新网页。

关于javascript - 创建简单的 Node js 服务器和客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5266535/

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