如何使所有连接的浏览器重装由服务器端事件启动 [英] How to make all connected browsers reload initiated by a server-side event
问题描述
假设有动态生成内容的网页 - 比如包含连接浏览器目前一些股利。当服务器的数量变化我想所有连接的浏览器重新加载计让每个人都看到了递增/递减。
Suppose there is a webpage with dynamically generated content -- say a div containing the current number of connected browsers. When the count changes on the server I want all connected browsers to reload the count so that everyone sees the increment/decrement.
什么是实现这一目标的最佳途径?
What's the best way to accomplish this?
关键词:AJAX,广播,浏览器,DIV,jQuery的
Keywords: ajax, broadcast, browser, div, jquery
推荐答案
下面是如何用ajax长轮询做服务器推送。浏览器发出一个Ajax请求,从而启动服务器端自投票。 AJAX请求保持打开状态,等待直到文件的变化的响应,并尽快,因为它得到的响应,它使一个新的长轮询请求。
Here's how to do server-push using ajax long-polling. The browser makes an ajax request which initiates server-side self-polling. The ajax request remains open, waiting for a response until the file changes, and as soon as it gets a response, it makes a new long-polling request.
下面是它看起来像 jQuery的和PHP,实施在html实时更新一个div显示的示例当前连接的客户端数量:
Here's what it looks like with jQuery and php, implementing the example of live-updating a div in the html showing the number of clients currently connected:
index.html的:
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:
longpolling.js:
$(document).ready(function() { connectToServer(1); });
function connectToServer( incp ) {
$.get("LongPolling.php",
{ inc: incp },
function(resp) {
$('#total').html(resp);
connectToServer(0);
}
);
}
LongPolling.php:
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);
}
?>
请注意:这不跟踪断开,所以它更像是实时跟踪的综合浏览量总数。 请参阅<一href="http://stackoverflow.com/questions/531001/running-server-side-function-as-browser-closes/531007#531007">http://stackoverflow.com/questions/531001/running-server-side-function-as-browser-closes/531007#531007对跟踪浏览器断开,也就是服务器端的操作的客户端断开连接的信息。
NOTE: This does not track disconnects, so it's more like live-tracking the total number of pageviews. See http://stackoverflow.com/questions/531001/running-server-side-function-as-browser-closes/531007#531007 for info on keeping track of browser disconnects, ie, server-side action on client disconnect.
这篇关于如何使所有连接的浏览器重装由服务器端事件启动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!