无需轮询的网页更新 [英] Web Page update without polling

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

问题描述

我正在开发一个网络应用程序,用户可以在其中请求服务,并且提供者可以响应他.因此,当用户请求某种服务时,我们的应用程序将向提供者发送通知(要求他响应用户).我想要做的是:当用户请求服务时,提供者会立即收到通知(就像 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/文档/Web/API/事件源

js

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

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,

然后它检查是否有什么改变...... db/file 什么

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

如果改变则输出数据

然后清除php缓存

等待 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)

编辑

sse 响应中的更多数据:

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

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