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

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

这一定非常容易与节点js todo,但我只是不知道从哪里开始。

在这里有没有人可以帮助和推动如何启动这个客户端和服务器。

非常感谢!

我已经看遍各地的东西来帮助我,但他们都最终失败….


UPDATE

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

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

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错误继续发生

如果你想得到一个体面的答案,你真的需要给我们更多的背景。

我假设你说'客户'是指一个networking浏览器。 如果我正确地理解了你的话,你会想把服务器上的数据推送到多个浏览器客户端。 在这种情况下,您将希望在浏览器和服务器之间保持持续连接 – 请检查Socket.io 。

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


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

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

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

确保重新启动Node.js服务器并刷新网页。