如何创建一个不由轮询驱动的聊天服务器?
我创建了一个由客户端轮询驱动的简单聊天服务器。 客户端每隔几秒发送一次数据请求,并收到任何新消息以及有关其对等方是否仍处于连接状态的信息。
由于客户端在移动平台(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
推送技术怎么样? 请参阅http://en.wikipedia.org/wiki/Comet_(programming)
How about push technology? see http://en.wikipedia.org/wiki/Comet_(programming)
我认为您描述的是 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.
您可能想查看这个项目,它使用各种技术,包括彗星。 发布详细信息位于此处,这是该页面的片段
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
我刚刚找到这篇文章我自己,它描述了以下技术(我在问题中提到):
I just found this article myself, which describes the following technique (which I referred to in the question):
我认为这几乎是不可能的,而且很危险。 互联网是无状态和无连接的,这意味着客户端和服务器之间的连接始终被视为不可靠。 这不是为了好玩。
通过尝试获得有状态的连接,您会引入新的问题。 特别是来自 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.