【问题标题】:create simple node js server and client创建简单的节点 js 服务器和客户端
【发布时间】:2011-07-13 02:22:16
【问题描述】:

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

使用 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 错误不断发生

【问题讨论】:

  • 您是否正在就您在客户端遇到的错误寻求具体帮助?如果是这样,您应该告诉我们错误。如果不是,那么您需要更具体地提出您的要求。
  • 可能是你的端口不对,看我更新的答案。
  • 我在这个问题上仍然需要帮助...

标签: javascript html node.js websocket


【解决方案1】:

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

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

【讨论】:

  • 我试过了,现在我不断收到:Failed to load resource :5454/socket.io/xhr-polling//1299797743814 Failed to load resource :5454/socket.io/xhr-polling//1299797753817 Failed to load resource :5454/socket.io/xhr-polling//1299797763822 Failed to load resource :5454/socket.io/xhr-polling//1299797773827 Failed to load resource :5454/socket.io/xhr-polling//1299797783830 Failed to load resource :5454/socket.io/xhr-polling//1299797793831 Failed to load resource 一遍又一遍......(不再有代码 301。)
  • 我也没有得到客户端连接的服务器端的响应
  • 无论出于何种原因,您的浏览器似乎无法访问 localhost:5454,您是否在与 Node.js 服务器相同的机器上运行浏览器?这是“本地主机”连接起作用的唯一方式。
  • 不,我不在同一个地方跑步。但我也尝试用服务器名称替换客户端 js 中的本地主机,我得到了相同的结果
  • 问题是您运行客户端的机器无法访问服务器(无论是根本还是在该特定端口)。听起来您没有自己调试网络问题的专业知识,因此您应该移动您的工作,以便它们都在同一台机器上。这将简化您的工作,并允许您像本教程一样使用“localhost”。将来,您需要在原始问题中包含此类信息。
猜你喜欢
  • 2016-06-20
  • 2014-02-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多