WebSocket跨连接通信(Tornado?)

发布于 2024-09-30 11:33:21 字数 539 浏览 7 评论 0原文

我对 WebSockets 进行了一些摸索,并且对运行一个进行基本 Websocket 连接的 Tornado 服务器是多么容易感到非常满意。我今天之前从未使用过 Tornado,虽然我喜欢我所看到的,但我对它的使用有一些疑问。

我主要使用 WebSocket,以便可以在两台或多台客户端计算机之间进行低开销的通信。 (出于对话的目的,我们就说它是一个聊天客户端)显然我可以从多台机器连接到服务器,并且它们都可以将消息推送到服务器并且服务器可以响应,这很棒!但这并不比标准 AJAX 请求好多少。如果我有持久连接,我也希望能够将数据推送到客户端。最简单的可能场景是用户 1 向服务器发布一条消息,服务器收到消息后立即将其推送给用户 2。

那么实现这一目标的好方法是什么?据我所知,在 Tornado 中,除了将消息放置在数据存储中的某个位置并让所有其他连接轮询新信息之外,没有其他方法可以在连接之间进行通信。不过,这让我觉得非常笨拙,因为此时您真正要做的就是将轮询过程从客户端转移到服务器。

当然,我在这里可能完全找错了对象。 Tornado 显然不是这项工作的正确工具,如果是这样的话,我很乐意听到替代方案的建议!

I'm fumbling around a bit with WebSockets, and was pretty pleased with how easy it was to get a Tornado server running that does basic websocket connections. I've never used Tornado before today, and while I like what I've seen there's a few questions that I have regarding it's use.

Primarily, I'm using WebSockets so that I can have low-overhead communications between two or more client machines. (For the purposes of conversation let's just say it's a chat client) Obviously I can connect into the server from multiple machines, and they can all push messages to the server and the server can respond, which is great! But that's not too much better than your standard AJAX requests. If I have a persistent connection I want to be able to push data to the clients as well. The simplest possible scenario is user 1 posts a message to the server and upon receiving it the server immediately pushes it to user 2.

So what would be a good way to accomplish that? As far as I can see in Tornado there's no way to communicate between connections other than placing the message in a datastore somewhere and having all the other connections poll for new info. That strikes me as terribly clunky though, because all you're really doing at that point is moving the polling process from the client to the server.

Of course, I may be barking up the wrong tree entirely here. It's certainly plausible that Tornado simply isn't the right tool for this job, and if that's the case I'd be happy to hear suggestions for alternatives!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

成熟稳重的好男人 2024-10-07 11:33:22

这是一个使用tornado、WebSockets和redis的聊天服务器: https://gist.github.com/pelletier/532067 更新:链接已修复,感谢@SamidhT)

Here is a chat server using tornado, WebSockets and redis: https://gist.github.com/pelletier/532067 (Updated: link fixed, thanks @SamidhT)

饭团 2024-10-07 11:33:22

尽管答案已经被接受:使用不同的服务对我来说仍然显得非常低效。为什么不直接使用共享内存+条件变量/信号量呢?你听起来像是遇到了标准的消费者-生产者问题

Though the answer has already been accepted: Using a different service still seems very inefficient to me. Why don't you just go with shared memory + conditional variables / semaphores? You sound like you got a standard Consumer-Producer problem

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文