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

时间:2011-03-10 22:10:24

标签: javascript html node.js websocket

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

对于节点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错误一直在发生

1 个答案:

答案 0 :(得分:1)

如果你想得到一个合适的答案,你真的需要给我们更多关于你在做什么的背景。

我假设当你说'客户'时你指的是网络浏览器。如果我理解你的话,你会想要将数据从服务器“推送”到几个网络浏览器客户端。在这种情况下,您将希望在浏览器和服务器之间保持持久连接 - 请查看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服务器并刷新网页。