时间:2021-07-01 10:21:17 帮助过:5人阅读
前面讲过一篇《实现一个简单的服务端推送方案-实例篇Polling(服务端阻塞读)》,那篇文章服务端利用Gearman队列来实现阻塞读,这篇服务器端用原生MySQL的Sleep函数来实现阻塞读,这篇文章也是另一篇文章《使用MySQL构建一个队列表》的实例实现。
客户端代码,JS库为prototype.js:
无标题文档
"."maxvid: ".$maxvid."/n", 3 , "/usr/local/apache2219/logs/php_log");$dblnk = mysql_connect('localhost:3306', 'root', 'cpyf');mysql_select_db('test', $dblnk);if ( $maxvid <= 0 ) { $result = mysql_query("select max(vid) from vdooropen"); $result = mysql_fetch_row($result); $maxvid = $result[0]; error_log(date("[Y-m-d H:i:s]")." > "."maxvid: ".$maxvid."/n", 3 , "/usr/local/apache2219/logs/php_log");}while (1){ $result = mysql_query("select * from vdooropen where vid > $maxvid order by vid limit 1"); $num = mysql_num_rows($result); if ( $num > 0 ) break; mysql_query("/*!999999 wait queue msg */ SELECT SLEEP(300)"); if ( mysql_errno() != 0 ) { mysql_close($dblnk); //必须先关闭 $dblnk = mysql_connect('localhost:3306', 'root', 'cpyf'); mysql_select_db('test', $dblnk); }}$result = mysql_fetch_row($result);// 返回 JSON 数组$response = array();$response['vid'] = $result[0];$response['msg'] = $result[0].",".$result[1].",".$result[2].",".$result[3].",".$result[4];$responseText = json_encode($response);error_log(date("[Y-m-d H:i:s]")." < ".$responseText."/n", 3 , "/usr/local/apache2219/logs/php_log");echo $responseText;flush();?>