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 (SwooleWebSocketServer $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} closedn"; });
出現(xiàn)錯誤:PHP Fatal error: Uncaught SwooleException: failed to listen server port[0.0.0.0:9502], Error: Address already in use
這是因為swoole調(diào)試由于開啟了進程守護,導(dǎo)至報錯
關(guān)掉進行就,在重新啟動就不會了
1 查看我的 swoole 監(jiān)聽端口號 9502
//查看端口 netstat -anp | grep 9502 //關(guān)掉守護 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)文章希望大家以后多多支持腳本之家!
版權(quán)聲明:
本站所有文章和圖片均來自用戶分享和網(wǎng)絡(luò)收集,文章和圖片版權(quán)歸原作者及原出處所有,僅供學(xué)習(xí)與參考,請勿用于商業(yè)用途,如果損害了您的權(quán)利,請聯(lián)系網(wǎng)站客服處理。