没有投票网页更新 [英] Web Page update without polling

查看:114
本文介绍了没有投票网页更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个web应用程序,用户可以在请求服务和供应商将提供给他回应。因此,当用户请求某种服务,我们的应用程序会发送通知给提供者(要求他响应用户)。 我正在试图做的是:当用户请求服务,提供者得到通知的瞬间(有点像Facebook并)

I'm developing a web app where a user can request a service and a provider will be available to respond to him. So, When a user requests for some sort of service, our application will send a notification to the provider (asking him to respond to the user). What I'm trying to do is: when a user requests a service, the provider gets the notification instantly (something like facebook does).

一个方法是使用AJAX来发送请求到服务器的每5-10秒获得本;我们所说的是轮询(到目前为止,我知道)。但是,这种方法有一定的缺陷,我看到: -

One way to get this is using AJAX to send requests to the server every 5-10 secs; what we call is polling (so far i know). But, this method has some defects, i see:-

  • 由于,该请求将被发送每5-10秒,这将是一个负载到服务器,以操纵请求并返回空数据(非常频繁)。
  • 同时其用武之地发送,即使没有任何更新对服务器的请求。
  • 想象一下,有人要求刚过服务器响应请求的服务。因此,供应商必须再等5-10秒得到通知,有人已要求一些东西。

所以,我想知道是否有一定的技术,我们可以在一个变化发生在我们的系统中不使用AJAX轮询请求即时更新我们的网页。

So, I wanted to know if there is some technique where we can update our web page instantly when a change occurs in our system without polling requests using AJAX.

推荐答案

下面是一个简单的服务器使用PHP发送的事件脚本。

here is a simple Server Sent Event Script using php.

支持

https://developer.mozilla.org/en-US/文档/网页/ API / EventSource的

JS

var sse=new EventSource("sse.php");
sse.onmessage=function(e){
 document.body.innerHTML=e.data;
};

sse.php

sse.php

header('Content-Type: text/event-stream'); // specific sse mimetype
header('Cache-Control: no-cache'); // no cache
while(true) {
 if(/*something changes*/){
  echo "id: ".time().PHP_EOL;
  echo "data: ".$data.PHP_EOL;
  echo PHP_EOL;
 }
  ob_flush(); // clear memory
  flush(); // clear memory
  sleep(10);// seconds 
}

这保持连接打开与客户端,

this keeps the connection open with the client,

然后它再检查,如果事情改变...数据库/文件无论

then it checks if something is changed ... db/file whatever

输出数据,如果改变了

,然后清除缓存PHP

and then clears the php cache

等待10秒,再次执行。

waits 10 seconds and does it again.

正如你可以看到客户端临危数据只有一些在服务器上修改

As you can see the client recieves the data only if something changes on the server

但我完全不知道服务器可以如何处理1000的人。

but i totally don't know how the server could handle 1000's of people.

Node.js的将是一个更好的办法。但要看什么语言你正在使用和/或如果你能真正使用Node.js的。

node.js would be a better way. but it depends what languages you are using and/or if you can actually use node.js.

的WebSockets是双向的。

websockets is both ways.

发送服务器的事件是单向的。(你需要这个)

server sent event is oneway.(you need this)

修改

上证响应里面更多的数据:

more data inside the sse response:

JS

var sse=new EventSource("sse.php");
sse.onmessage=function(e){
 console.log(JSON.parse(e.data))
};

sse.php

sse.php

header('Content-Type: text/event-stream'); // specific sse mimetype
header('Cache-Control: no-cache'); // no cache
while(true) {
 if(/*something changes*/){
  echo "id: ".time().PHP_EOL;

  $dataArray=array('id'=>$id,'name'=>$name,'more'=>$more);
  echo "data: ".json_encode($dataArray).PHP_EOL;

  echo PHP_EOL;

 }
  ob_flush(); // clear memory
  flush(); // clear memory
  sleep(10);// seconds 
}

如果你需要一些更多的信息只是要求

if you need some more info just ask

这篇关于没有投票网页更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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