简单的 iPhone 聊天应用程序
我正在使用 Titanium 开发一个 iPhone 应用程序,其中有一个 iPhone 用户之间的小型聊天组件(不是短信,而是实际的客户端-服务器聊天)
这是我的方法:我的后端将在 ASP.NET 中,每次用户发送一条消息,它会存储[message_id, from_id, to_id, message_content],然后接收者将有一个计时器,每5秒刷新一次,以从ASP.NET服务器数据库中获取新消息。
但这引起了人们的担忧。如果我继续每 5 秒刷新一次,它将不断地传输并耗尽接收器的带宽。当有新消息时,是否有更好的方法来实现侦听器/接收器?
我听说过套接字编程,但是对于这种情况来说这是一个好方法吗?我该如何开始使用它?
谢谢
I am using Titanium to develop an iPhone application, in which has a small chat component between the iPhone users (not SMS, but actual client-server chat)
This is my approach: my back end will be in ASP.NET, every time a user sends a message, it will store [message_id, from_id, to_id, message_content], and then the receiver will have a timer that refreshs every 5 seconds to pick up new messages from the ASP.NET server database.
But this raises a concern. If I keep doing a refresh every 5 seconds, it will constantly stream and exhaust the receiver's bandwidth. Is there a better to way to implement a listener/receiver when there is a new message?
I have heard of socket programming but is it a good approach for this situation and how do i get started with it?
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不仅会使用带宽,还会消耗电池电量。请改用推送通知。
You won't just use bandwidth, you'll eat up battery power too. Use push notifications instead.
建议仅在应用程序在后台运行时使用推送通知,而不是在用户实际聊天时使用推送通知。这就是推送通知最初设计的目的。
通过 http 轮询服务器是一个很好的解决方案,您可以使用一些节省带宽的技术,例如根据用户活动更新轮询频率(几分钟后没有聊天,将轮询时间减少到 30 秒)。
您不能在聊天应用程序中使用推送通知,因为您无法每分钟获取如此多的通知以使聊天看起来是实时的。对服务器的简单 http 轮询可以小到一个二进制包,并且不会显着影响用户的带宽。
It would be advisable to use push notifications only for when the application is running in background, not for when the user is actually chatting. That's what push notifications were designed for in the first place.
Polling the server via http is a good solution and there are techniques to save bandwith that you can use such as updating the frequency of the polling depending on user activity (no chats since a few minutes, reduce the polling time to 30 seconds).
You cannot use push notifications for a chat application because you cannot obtain so many notifications per minute to make the chat appear to be in real-time. And a simple http poll to a server can be as small as one binary package and not affect the user's bandwith significantly.
为什么不直接使用苹果的推送通知系统呢?
每当用户收到消息时,您的后端都可以向 iPhone 发送推送通知,iPhone 会在收到推送时下载新消息,或者如果消息不太长,则直接在推送通知中发送消息
Why don't you just use Apple's push notification system?
Whenever a user receives a message your back end can send a push notification to the iphone and the iphone either downloads new messages whenever it receives a push, or if the message isn't too long you just send the message directly in the push notification