javascript 创建简单的节点 js 服务器和客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5266535/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 16:35:22  来源:igfitidea点击:

create simple node js server and client

javascripthtmlnode.jswebsocket

提问by Naftali aka Neal

What i want to be able to do is have a server which one client will send updates and multipleother clients would receive those updates and update the relevant parts of the page.

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

This must be really easy todo with node js, but i just dont know where to get started.

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

Is there anyone here that can help and give me a push of how to startup this client and server.

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

Thank so much!

非常感谢!

I have looked all over for something to help me but they all end up failing....

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



UPDATE

更新

I want to use socket.io and nodeJS to make the connections.

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

I have starter code for the server which i got online:

我有我上网的服务器的启动代码:

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

also i have code for the client. but it has errors:

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

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

UPDATE 2:

更新 2:

This is what happens when i run the server:

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

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

ok and this is what i see in the console in chrome when i run the client:

好的,这就是我在运行客户端时在 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)

And those XHR error keep happening

而那些 XHR 错误不断发生

采纳答案by Chris W.

You really need to give us more context to what you are doing if you want to get a decent an answer.

如果你想得到一个像样的答案,你真的需要为我们提供更多关于你正在做的事情的背景信息。

I assume when you say 'client' you are referring to a web browser. And if I understand you correctly you're going to want to be 'pushing' data from the server to several web browser clients. In which case you are going to want to keep a persistent connection open between the browser and server--check out Socket.io.

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

The basic flow will be (1) a user visits your webpage, (2) some javascript which lives on that webpage will connect to your Node.js/Socket.io server, (3) the server can then push messages to any/all of the currently connected browsers AND the browser can send messages to the server, (4) for messages coming from the server the Javascript running on each client can interpret those messages and make the appropriate changes to the web page's DOM.

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



EDIT:Your client side javascript can't connect to the server. If they are running on the same machine (localhost) then that probably means the port is the problem.

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

Try specifying the 'port' option for your client, which in your case is '5454'.

尝试为您的客户端指定“端口”选项,在您的情况下为“5454”。

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

Be sure to restart the Node.js server AND refresh the webpage.

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