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

查看:101
本文介绍了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一个发送消息(名称,消息和target(client2))-服务器-客户端2接收消息(来自client1,消息,client1.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 ..

我有很多有关长轮询的示例,例如:如何实现基本的"; Long Polling"?

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套接字.

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 Web worker来调用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.

使用网络工作者的原因是,他们在检查新消息时不会阻塞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:

使用网络工作者

Using the webworker

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

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

worker.postMessage("start");

网络工作者(worker.js)

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天全站免登陆