PHP之使用swoole統(tǒng)計在線人數(shù)和ID案例講解
$server->connections
// s e r v e r − > c o n n e c t i o n s 遍 歷 所 有 w e b s o c k e t 連 接 用 戶 的 f d , 給 所 有 用 戶 推 送 統(tǒng) 計 人 數(shù) : c o u n t ( server->connections 遍歷所有websocket連接用戶的fd,給所有用戶推送 統(tǒng)計人數(shù):count( server−>connections遍歷所有websocket連接用戶的fd,給所有用戶推送統(tǒng)計人數(shù):count(server->connections)
例子:
在開啟或關(guān)閉時統(tǒng)計在線人數(shù)
開啟
$server->on('open', function (Swoole\WebSocket\Server $server, $request) { $arr = array('action'=>'count','num'=>count($server->connections)); $jsonTo = json_encode($arr); echo "在線人數(shù):{$jsonTo};server: handshake success with fd={$request->fd}\n"; });
關(guān)閉
$server->on('close', function ($ser, $fd) { $arr = array('action'=>'count','num'=>count($server->connections)); $jsonTo = json_encode($arr); echo "在線人數(shù):{$jsonTo};client {$fd} closed\n"; });
出現(xiàn)錯誤:PHP Fatal error: Uncaught Swoole\Exception: failed to listen server port[0.0.0.0:9502], Error: Address already in use
這是因為swoole調(diào)試由于開啟了進(jìn)程守護(hù),導(dǎo)至報錯
關(guān)掉進(jìn)行就,在重新啟動就不會了
1 查看我的 swoole 監(jiān)聽端口號 9502
//查看端口 netstat -anp | grep 9502 //關(guān)掉守護(hù) kill -9 174739
再重啟服務(wù)
參考:
https://wenda.swoole.com/detail/106719
http://www.51zuso.com/admin/p/710.html
到此這篇關(guān)于PHP之使用swoole統(tǒng)計在線人數(shù)和ID案例講解的文章就介紹到這了,更多相關(guān)PHP之使用swoole統(tǒng)計在線人數(shù)和ID內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PHP圖片等比縮放類SimpleImage使用方法和使用實例分享
這篇文章主要介紹了PHP圖片等比縮放類SimpleImage使用方法和使用實例分享,需要的朋友可以參考下2014-04-04