Apache、javascript 和 PhoneGap:收听一些随机事件的最佳方式是什么?间隔还是长轮询? [英] Apache, javascript and PhoneGap : What's the best way to listen to some random event ? Interval or long polling?

查看:15
本文介绍了Apache、javascript 和 PhoneGap:收听一些随机事件的最佳方式是什么?间隔还是长轮询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个应用程序,其中客户端 (PhoneGap) 正在等待某个事件从另一个客户端触发!

I'm building an app in which the client (PhoneGap) is waiting for some event to be triggered from another client !

所以模式当然是:Client1 发送一条消息(名称、消息和目标(客户端 2)) - 服务器 - 客户端 2 接收消息(来自客户端 1、消息、客户端 1.name)这可能每秒或每小时发生一次.没有规则.它也可以只发生一次或永远不会发生..

So of course the schema would be: Client1 one send a message (name, message and target(client2)) - Server - Client 2 receive the message (from client1, message, client1.name) this can happen every second or every hour. No rules. It can also happen only once or never ..

我有很多关于长轮询的例子,比如这里:如何实现基本的"长轮询?

I've a lot of examples about long polling like here: How do I implement basic "Long Polling"?

我知道在 Apache 中使用它很糟糕.所以我想知道在这种情况下我是否应该使用 setInterval 代替?

I know it's bad to use it with Apache. So I'm wondering if in this case I should use setInterval instead?

如果我之前记录了从一个客户端到另一个客户端的任何消息并传递它,我是否应该设置一个时间间隔来要求服务器检查数据库?或者这是一个不好的方法?

Should I set up an interval to ask the server to check the DB if I previously recorded any message from a client to another one and deliver it ? or is this a bad way to go?

请指教,谢谢:)

推荐答案

这取决于消息的发送频率以及您是否需要实时处理它们.如果是这种情况,我会考虑实现 HTML 5 Web Sockets.

This will depends on the frequency in which the messages are being sent and if you require to handle them in real-time basis. If that is the case I would consider implementing HTML 5 Web Sockets.

这里是一篇可以帮助您的好文章开始,如果你读到最后,你会发现还有一个跨浏览器问题的解决方案(使用 socket.io 库),这将回退到长轮询和其他技术来实现您想要的.

Here is a good article that could get you started, if you read it to the end you will see that there is a solution to cross browser issues as well (using socket.io library) that would fallback to long polling and other techniques to achieve what you want.

如果您不需要实时处理消息,并且您的应用程序可以等待一段时间以根据已存储在存储库(例如数据库)中的消息执行某些操作,我会使用带有 setIntervaljavascript 网络工作者来调用 REST 服务检查这些消息.这里是一篇与此相关的好文章.

If you don't need to handle the messages in real-time and is ok for your application to wait a certain period of time to execute some action based on a message already stored in a repository such as a Database, I would use javascript web workers with a setInterval to call a REST service checking for those messages. Here is a good article related to that.

使用 web worker 的原因是它们在检查新消息时不会阻塞 UI,因此可用性会更好.

The reason to use web workers is that they won't block the UI while checking for new messages, so the usability will be better.

这是一个网络工作者的小例子:

Here is a small example of a web worker:

使用网络工作者

var worker = new Worker("http://mydomain.com/worker.js");

worker.onmessage = function(event) {
   //new message received, do something in the UI
}

worker.postMessage("start");

Web Worker (worker.js)

this.onmessage = function(event) {
   postMessage("worker started");
}

//Period of time to check for the messages
setInterval(function() { getNewMessage() }, 5000);

function getNewMessage() {
   //Call your REST service here and respond back to the main js thread if a new
   //message is received
   postMessage("New message received!");
}

希望能帮到你

这篇关于Apache、javascript 和 PhoneGap:收听一些随机事件的最佳方式是什么?间隔还是长轮询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆