如何创建一个不由轮询驱动的聊天服务器?

发布于 2024-07-29 03:12:57 字数 368 浏览 6 评论 0原文

我创建了一个由客户端轮询驱动的简单聊天服务器。 客户端每隔几秒发送一次数据请求,并收到任何新消息以及有关其对等方是否仍处于连接状态的信息。

由于客户端在移动平台(iPhone)上运行,因此我一直在寻找摆脱轮询的方法,因为轮询会很快耗尽电池电量。 我读到可以无限期地保持 http 连接打开,但不明白如何在实践中利用这种技术。 我还想知道这种连接是否足够稳定,可以在移动环境中使用。

理想的情况是,服务器仅在发生影响客户端的事件(例如对等方发布消息或离线)时才向客户端发送数据。

是否建议尝试通过 http 来完成此任务,或者我必须通过 tcp 编写自己的协议? 根据我的需要定制 xmpp 有多难(我的聊天服务器有一些我必须轻松实现的专门功能)。

I have created a simple chat server that is driven by client polling. Clients send requests for data every few seconds, and get handed any new messages as well as information about whether their peer is still connected.

Since the client is running on a mobile platform (iPhone), I've been looking for ways of getting rid of the polling, which quickly drains the battery. I've read that it's possible to keep an http connection open indefinitely, but haven't understood how to utilize this technique in practice. I'm also wondering whether such connections are stable enough to use in a mobile setting.

The ideal scenario would be that the server only sends data to clients when an event that affects them has occurred (such as a peer posting a message or going off line).

Is it advisable to try to accomplish this over http, or would I have to write my own protocol over tcp? How hard would it be to customize xmpp to my need (my chat server has some specialized features that I would have to easily implement).

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

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

发布评论

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

评论(5

谁对谁错谁最难过 2024-08-05 03:12:57

我认为您描述的是 XMPP 而不是 BOSH。

http://xmpp.org/extensions/xep-0206.html

我已经在非移动设备上的聊天服务器和 JavaScript 客户端之间使用了这种 http 绑定方法。 这对我来说效果很好。

I think you're describing XMPP over BOSH.

http://xmpp.org/extensions/xep-0206.html

I've used this http-binding method between a chat server and javascript client on non-mobile devices. It worked well for me.

寂寞清仓 2024-08-05 03:12:57

您可能想查看这个项目,它使用各种技术,包括彗星。 发布详细信息位于此处,这是该页面的片段

我很高兴能够
宣布首次公开放映
我一直在做的一个项目
在上个月的空闲时间或
二、新的基于Web的IRC聊天
应用程序。

这个项目汇集了很多
必须采用的新技术
开发使其成为可行的,
可扩展且高效。

一些底层工具旨在
我认为使这成为可能
“足够稳定”已经发布,
例如 php Socket Daemon 库
我写信是为了能够处理
数百至数千
“Comet”http 连接,以及同等的
IRC 客户端连接数量。

You might like to check out this project which uses a variety of techniques including Comet. Release details are here, here's a snippet from that page

It’s my distinct pleasure to be able
to announce the first public showing
of a project that I’ve been working on
in my spare time in the last month or
two, a new Web Based IRC chat
application.

This project brings together a lot of
new technologies which had to be
developed to make this a feasible,
scalable and efficient.

Some of the underlying tools build to
make this posible that i consider
’stable enough’ are already released,
such as the php Socket Daemon library
i wrote to be able to deal with
hundreds up to many thousands of
“Comet” http connections, and an equal
amount of IRC client connections.

揪着可爱 2024-08-05 03:12:57

我刚刚找到这篇文章我自己,它描述了以下技术(我在问题中提到):

...让客户端发出 HTTP 请求
并让服务器保存请求
在队列中直到有消息为止
推。 如果 TCP/IP 连接是
丢失或超时,客户端将
发出新的 HTTP 请求,以及延迟
只会是一个往返时间
请求/响应对。 。 。 这个型号
实际上需要两个 TCP/IP
HTTP 连接,客户端到
服务器,尽管不是永久的并且
因此适合移动设备

I just found this article myself, which describes the following technique (which I referred to in the question):

... have the client make an HTTP request
and have the server hold the request
on the queue until there is a message
to push. if the TCP/IP connection is
lost or times-out, the client will
make a new HTTP request, and the delay
will only be the round trip time for a
request/response pair . . . this model
effectively requires two TCP/IP
connections for HTTP, client to
server, though none permanent and
hence mobile friendly

梨涡 2024-08-05 03:12:57

我认为这几乎是不可能的,而且很危险。 互联网是无状态和无连接的,这意味着客户端和服务器之间的连接始终被视为不可靠。 这不是为了好玩。

通过尝试获得有状态的连接,您会引入新的问题。 特别是来自 3g 应用程序。 如果连接中断怎么办? 您无法控制服务器,也无法推送。

我认为发送短信/文本消息并拥有一个处理该消息的应用程序会更容易。

I think this is nearly impossible and dangerous. The internet works stateless and connectionless meaning that the connection between client and server is always handled as unreliable. And this is not for fun.

By trying to get a stateful connection you are introducing new issues. Especially from a 3g application. What if the connection breaks? You have no control over the server and cannot push.

I think it would even be easier to send sms/text messages and have an application that handles that.

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