抛出问题:web浏览器如何与服务保持通信?

方法一:Ajax轮询

方法二:EventSource轮询

方法三:websocket保持长连接

下面的解决方案是,Ajax轮询与EventSource轮询的合体。

客户端代码:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="jquery.min.js"></script>
<script src="PollSource.js"></script>
</head>
<body>
<h1>Server Data:</h1>
<div id="serverData"></div>
<a href="javascript:Poll.open(1000);">open poll</a>
<a href="javascript:Poll.close();">close poll</a>
<script type="text/javascript">
var serverData,Poll;
var SERVER_URL = "index.php";
window.onload = function(){
serverData = document.getElementById("serverData");
Poll = new PollSource(SERVER_URL);
Poll.onmessage = function(json) {
//console.log(json);
serverData.innerHTML = json.data;
}
}
</script>
</body>
</html>

PollSource.js代码:

/**
* PollSource javascript轮询服务器
* @param string url 服务器地址 例:www.example.com/index.php
* @param json jsonParam 传递给服务器的参数
* @author Echo
*/
function PollSource(url,jsonParam) {
this.url = url;
this.onmessage = null;
this.resource = null;
this.PollType = typeof(EventSource)=='undefined' ? 'AjaxGet' : 'EventSource';
//将轮询类型传递给服务器,以便服务器作出响应
if(typeof jsonParam == 'object') {
jsonParam['PollType'] = this.PollType;
}else{
jsonParam = {PollType:this.PollType};
}
this.url += '?';
for(var i in jsonParam) {
this.url += i+'='+jsonParam[i]+'&';
}
this.url = this.url.substring(0,(this.url.length-1));
/**
* 开始轮询
*/
this.open = function() {
var onmessage = this.onmessage;
var url = this.url;
var msg = '连接没有被建立,或者已经关闭,或者发生了某个致命错误'; //错误时不友好提示 :)
if(this.PollType == 'AjaxGet') {
//采用轮询
this.resource = {};
url += '&Seconds=';
var hangUp = false; //是否挂起
var retry = spend = 3000; //最快轮询速度 3 秒一次
this.resource.lockInt = setInterval(function(){
retry -= spend;
//alert(retry);
if(retry > 0 || hangUp) return;
hangUp = true; //挂起
$.get(url+(new Date()).getSeconds(),{},function(data){
try{
var json = JSON.parse(data);
retry = json.retry;
hangUp = false; //取消挂起
if(typeof onmessage == 'function') onmessage(json);
}catch(e){console.log(e);alert(msg);}
});
},spend);
this.resource.close = function() {
if(typeof this.lockInt == 'number') {
clearInterval(this.lockInt);
}
}
}else{
//采用服务器发送事件技术
this.resource = new EventSource(this.url);
this.resource.onmessage = function(e) {
try{
var json = JSON.parse(e.data);
if(typeof onmessage == 'function') onmessage(json);
}catch(e){
console.log(e);alert(msg);
}
}
this.resource.onerror = function(e) {
if(this.readyState == 2) alert(msg);
}
}
}
/**
* 关闭轮询
*/
this.close = function() {
if(this.resource) this.resource.close();
}
}

服务端代码:

<?php
header("Content-type: text/html; charset=utf-8");
date_default_timezone_set("Asia/Shanghai");
$retry = 9000; //AjaxGet 轮询 最快是 3000 所以这里设置的值最好是 3 的倍数 如果不是三的倍数 那么时间误差在1~2秒内
$result = array('code'=>0,'msg'=>'拉取成功','retry'=>$retry,'data'=>'当前时间是:'.date('Y-m-d H:i:s'));
$result = json_encode($result);
$PollType = isset($_GET['PollType']) ? trim($_GET['PollType']) : '';
sleep(3); //延迟 3秒 返回数据 重试时间 9秒 刚好是 客户端12秒一次刷新数据
if($PollType == 'AjaxGet') {
//header('Content-Type:application/json; charset=utf-8');
echo $result;
}else if($PollType == 'EventSource') {
header('Content-Type: text/event-stream');
echo 'data:'.$result."\n\n";
echo 'retry:'.$retry."\n\n"; //EventSource轮询速度 虽然可以设定为 1 秒 一次 但不建议
flush();
}else{
die('error');
}

测试效果:

代码下载:百度网盘

参考资源:

https://developer.mozilla.org/zh-CN/docs/Server-sent_events/EventSource
https://developer.mozilla.org/zh-CN/docs/Server-sent_events/Using_server-sent_events#Event_stream_format

javascript轮询请求服务器的更多相关文章

  1. Ajax轮询请求

    Ajax轮询请求 什么是轮询? 轮询(polling):客户端按规定时间定时向服务端发送ajax请求,服务器接到请求后马上返回响应信息并关闭连接. Ajax轮询需要服务器有很快的处理速度与快速响应. ...

  2. 常规轮询请求,客户端用Ajax调webservice的方法

    服务端发布webservice,下图方框中的一定要有 客户端代码 <script type="text/javascript"> $(document).ready(f ...

  3. 实现Comet(服务器推送)的两种方式:长轮询和http流

    Comet 是一种高级的Ajax技术,实现了服务器向页面实时推送数据的技术,应用场景有体育比赛比分和股票报价等. 实现Comet有两种方式:长轮询与http流 长轮询是短轮询的翻版,短轮询的方式是:页 ...

  4. 利用ajax短轮询+php与服务器交互制作简易即时聊天网站

    主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Server-sent Events). 本文主要介绍ajax短轮询的简易实现方式. 看懂此文 ...

  5. ajax短轮询+php与服务器交互制作简易即时聊天网站

    主流的Web端即时通讯方案大致有4种:传统Ajax短轮询.Comet技术.WebSocket技术.SSE(Server-sent Events). 本文主要介绍ajax短轮询的简易实现方式. 看懂此文 ...

  6. Comet 反Ajax: jQuery与PHP实现Ajax长轮询

    原文地址(http://justcode.ikeepstudying.com/2016/08/comet-%E5%8F%8Dajax-%E5%9F%BA%E4%BA%8Ejquery%E4%B8%8E ...

  7. polling轮询和comet

    comet:(原意:彗星) Comet is a web application model in which a long-held(held:保留) HTTP request allows a w ...

  8. .Net MVC 实现长轮询

    什么是长轮询? 长轮询是“服务器推”技术实现方式的一种,可以将服务端发生的变化实时传送到客户端而无须客户端频繁的地刷新.发送请求. 长轮询原理? 客户端向服务器发送Ajax请求,服务器接收到请求后,保 ...

  9. 10分钟学会web通讯的四种方式,短轮询、长轮询(comet)、长连接(SSE)、WebSocket

    一般看到标题我们一般会产生下面几个问题??? 什么是短轮询? 什么是长轮询? 长连接又是什么? wensocket怎么实现呢? 他们都能实现web通讯,区别在哪呢,哪个好用呢? 接下来我们就一个个来了 ...

随机推荐

  1. (转)Python_如何把Python脚本导出为exe程序

    原文地址:https://www.cnblogs.com/robinunix/p/8426832.html 一.pyinstaller简介 Python是一个脚本语言,被解释器解释执行.它的发布方式: ...

  2. C# vb .net实现拉伸效果滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的拉伸效果呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步 ...

  3. c# js 时间

    DateTime GetTime(double timeStamp) { DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new Dat ...

  4. VUE回顾基础3

    1.方法 在vue模板里函数被定义为方法来使用,将函数放在methods对象里,作为一个属性,就可以在模板里使用它 this:在方法中this指向该方法所属的组件,可以使用this方文档data对象的 ...

  5. Vue学习之项目部分代码(十八)

    1.mian.js: // 入口文件 import Vue from "vue"; // 1.1导入路由 import VueRouter from "vue-route ...

  6. JVM粗解

    主要是碰到了eclipse无法启动的问题.之前不知道怎么回事导致eclipse启动速度一次比一次慢, 百度了下开始改动eclipse.ini参数 也不知道改了啥.第二天直接起不来eclipse了. 于 ...

  7. 爬取网易云音乐歌手和id

    pip install lxml csv requests from lxml import etree from time import sleep import csv import reques ...

  8. 【RAC】rac环境下的数据库备份与还原

    [RAC]rac环境下的数据库备份与还原 一.1  BLOG文档结构图 一.2  前言部分 一.2.1  导读 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~ ...

  9. 无法将“ng”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。 通用解决方案

    1.找到你安装的路径 以@angular/cli 为例 (找到 ng.cmd 这个指令的具体位置) 2. 右键 这台电脑 添加路径到 系统变量的 Path中, 如下图 3.关闭所有的 cmd,并重新打 ...

  10. 【Spring Boot】Spring Boot之自定义配置参数绑定到Java Bean

    一.@Value方式 1.我的配置文件:application-dev.yml # 自定义项目配置 startproject: pro1: pro2: pro3: pro4: lists: - ' - ...