使用Ajax或插口推拉技术 [英] push and pull technologies using Ajax or Socket

查看:107
本文介绍了使用Ajax或插口推拉技术的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网站,需要实时同Facebook发送到在线客户通知,经过google搜索,我发现了很多关于推动文档和拉技术。我发现这个文档的方式使用Ajax或者套接字实现它们。我需要知道什么是最好的在我的情况下使用,它是如何使用JavaScript或jQuery和PHP codeD。

I have a website that needs to send notifications to the online clients at real time same as Facebook, after more googling, I found a lot of documentation about push and pull technology. I found from this documentation ways for implementing them using Ajax or Sockets. I need to know what is the best to use in my case and how is it coded using javascript or jquery and php.

推荐答案

如果你能保证将只有单一的浏览器打开每个登录的用户,那么你可以很容易地应用此长轮询技术。

If you can ensure that there will be only single browser open per logged in user then you can apply this long polling technique easily.

政策AJAX调用:

  1. 请不要做出要求每2秒。
  2. 别急,只有2秒获得由previous请求响应后作出的要求。
  3. 如果请求没有在12秒内作出反应,那么请不要等到发送一个新的请求。这是连接丢失情况。

策略服务器响应:

  1. 如果马上有更新响应。要检查是否有更新依靠会议; (更好,如果你可以发送一些提示从客户端接收到的类似的最新消息,本次更新检查机制将消除露天上述单个浏览器的限制)

  1. if there is update response immediately. to check if there is update rely on session ; (better if you could send some hint from client side like latest message received; this second update checking mechanism will eliminate the restriction of single browser open as mentioned above)

否则睡眠()1秒; (不要用无限循环,但使用休眠),然后检查是否有更新;如果更新是有反应;如果不睡觉再服1次;重复,直到整个10秒的位置经过再没有更新进行回应

otherwise sleep() for 1 second; (do not use infinite loop but use sleep) and then check whether there is update; if update is there respond; if not sleep again for 1 second; repeat this until total 10 seconds has elapsed and then respond back with no update

如果您应用此策略(俗称长轮询),你会发现处理器的使用率从95%在重负载的情况下降低到4%。

If you apply this policy (commonly known as long polling), you will find processor usage reduced from 95% to 4% under heavy load case.

希望这解释。祝您好运。

Hope this explains. Best of luck.

这篇关于使用Ajax或插口推拉技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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