栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何通过服务器端事件重新启动所有连接的浏览器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何通过服务器端事件重新启动所有连接的浏览器

这是使用ajax长轮询进行服务器推送的方法。浏览器发出一个ajax请求,该请求启动服务器端自轮询。Ajax请求保持打开状态,等待响应,直到文件更改为止,并​​且一旦获得响应,它就会发出一个新的长轮询请求。

这是jQuery和php的外观,实现了在html中实时更新div的示例,该示例显示了当前连接的客户端数量:

index.html:

<html><head><title>Comet Test</title>  <script type="text/javascript" src="jquery.js"></script>  <script type="text/javascript" src="longpolling.js"></script></head><body>  Number of connected users: <div id="total">0</div></body></html>

longpolling.js:

$(document).ready(function() { connectToServer(1); });function connectToServer( incp ) {  $.get("LongPolling.php",        { inc: incp },        function(resp) {          $('#total').html(resp);          connectToServer(0);        }       );}

LongPolling.php:

<?php# (over)write file with contents, locking the file while doing so.# just barf and die if there's an error.function update($file, $contents){  $f = fopen($file, 'w');  if(!$f) { echo "ERROR1"; exit; } # couldn't open file for writing.  if(!flock($f, LOCK_EX)) { echo "ERROR2"; exit; } # couldn't get lock.  fwrite($f, $contents);  fclose($f);  # this also releases the lock.  return $contents;}# fetch the contents of the given file.# if the file doesn't exist, create it with contents "0"function fetch($file){  if(file_exists($file)) {    if(!is_readable($file)) { echo "ERROR3"; exit; }    $x = file_get_contents($file);  } else {    $x = 0;    update($file, $x);  }  return $x;}$fc = 'connx.txt';   # file that stores the number of connections.if ( $_REQUEST['inc'] == 1 ) {  # someone just connected.  echo update($fc, fetch($fc)+1);} else {  # someone is reconnecting (also happens immediately after connect).  $last = filemtime($fc);  do {  # wait until some other instance causes $fc to change...    sleep(1);    clearstatcache(); # otherwise filemtime() results are cached!  } while(filemtime($fc) == $last);  echo fetch($fc);}?>

注意:这不会跟踪断开连接,因此它更像是实时跟踪浏览量的总数。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/569075.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号