使用带有 TCP 的 Net Stream 对象效果很好(如 node.js 介绍视频中的预设),但是我该怎么办这在 HTTP 中?
有没有办法在 http.createServer()
中访问套接字/客户端?或者说有什么方法可以实现呢?我试图从官方 节点聊天演示 节点聊天演示中找出解决方案。 com/ry/node_chat" rel="noreferrer">源代码,但我不太明白。
我了解客户端 js,但是当我(作为客户端)通过 AJAX 向服务器端 js 发送消息后会发生什么?我怎样才能发送给服务器上的其他客户端?
请注意,我不想学习该过程的逻辑,所以我不想使用socket.io或任何其他框架、库、模块。
非常感谢您的帮助!
With the Net Stream Object with TCP works great (as presetend in the node.js introduction video), but how should I do this in HTTP?
Is there a way to access sockets/clients within an http.createServer()
? Or what's the method to do it? I tried to figure out the solution from the official node chat demos souce code, but I don' really understand.
I understand the client side js, but what's happening after I(as a client) send a message trough AJAX, to the server side js? How can I send to the other clients who's on the server too?
Please note that I wan't to learn the logic of the process, so I don't want to use socket.io or any other frameworks, libraries, modules.
Thanks very much for any help!
发布评论
评论(3)
理想情况下,您只需使用
WebSockets
,但替代方案是 ajax 长轮询。您可以使用一种称为长轮询的技术来进行聊天。这意味着您向服务器发出(ajax)请求,服务器会保留该请求,直到有一些数据要发送为止。
因此,客户端最终会定期轮询服务器,如果服务器没有新消息,它只会保留您的请求。如果有消息,则会将其发送回客户端,然后客户端将再次轮询服务器。
[[伪代码]]
// Client.js
// server.js
更好的推送技术是 服务器端事件。请参阅此处的示例。但这确实需要浏览器支持(我认为是 Chrome 和 Opera)。
Ideally you just use
WebSockets
but the alternative is ajax long polling.You can use a technique known as long polling to do the chat. This means you make an (ajax) request to the server and the server keeps hold of this request until it has some data left to send.
So the clients ends up periodically polling the server, if the server has no new messsages, it just keeps hold of your request. If it has a message it sends it back to the client and the client will poll the server again.
[[Pseudo Code]]
// Client.js
// server.js
A better push technology would be Server-side events. See an example of it here. This does require browser support though (Chrome and opera I think).
一种方法是客户端“订阅”充当消息分发者的通道。订阅后,客户端就会收到发送到频道的每条消息的副本。
许多节点聊天服务依赖于 Redis 的 pubsub 功能来处理从一个客户端到任意数量客户端的消息分发。如果您想“自己动手”,那么了解 Redis 如何解决这个问题将是一个很好的开始。
One way of doing it involves clients "subscribing" to a channel that acts as a distributor for messages. Once subscribed, a client then receives a copy of each message sent to the channel.
Many node chat services rely on redis' pubsub feature to handle this distribution of messages from one to any number of clients. If you wanted to "roll your own", understanding how redis solves this problem would be a great start.
如果您想了解长轮询的基本原理,请尝试查看这篇文章。我总结了我自己的长轮询服务器的某些部分,以及我如何实现它们,并且本文还包含其他资源的链接。它至少应该让您更全面地了解轮询的工作时间。
如果您想学习逻辑以便享受 Node.js 的编码乐趣,而不是使用现有的解决方案,那么我建议您逐步从最简单和基本的实现到更复杂的内容。不要试图从第一次尝试就构建整个事情,因为这是最容易失败的方式之一。
If you want to know the basic principles of long polling then try to look at this article. I summarized there certain parts of my own long poll server, how I implemented them and the article also contains links to other resources. It should give you at least the bigger picture of how long polling works.
If you want to learn the logic in order to have some coding fun with node.js, and not to use existing solutions, then I would recommend to go step by step from the most simple and basic implementation to more complex stuff. Don't try to build the entire thing from the first shot because it's one of the most surest way how to fail.