有很多文章,想知道每個文章的在線人數(shù)
private function start()
{
// 初始化worker類
$worker = new Worker('websocket://0.0.0.0:2346');
$worker->count = 1;
// 統(tǒng)計連接數(shù)
$total_connections = 0;
$worker->onWorkerStart = function (Worker $ws_worker) {
// 每隔[30]秒鐘向所有客戶端推送一次隨機數(shù)據(jù)
Timer::add(30, function () use ($ws_worker) {
foreach ($ws_worker->connections as $connection) {
global $total_connections;
// 處理消息
$json = [];
$json['type'] = 'workerstart';
$json['worker_id'] = $connection->id;
$json['online'] = $total_connections;
$json['ip'] = $connection->getRemoteIp();
$json['time'] = date('Y-m-d H:i:s');
$connection->send(json_encode($json));
echo (json_encode($json));
echo (PHP_EOL);
}
});
};
// 連接回調(diào)
$worker->onConnect = function (TcpConnection $connection) {
global $total_connections;
$total_connections++;
// 處理消息
$json = [];
$json['type'] = 'connect';
$json['worker_id'] = $connection->id;
$json['online'] = $total_connections;
$json['ip'] = $connection->getRemoteIp();
$json['time'] = date('Y-m-d H:i:s');
// 輸出
$connection->send(json_encode($json));
echo (json_encode($json));
echo (PHP_EOL);
};
// 關(guān)閉回調(diào)
$worker->onClose = function (TcpConnection $connection) {
global $total_connections;
$total_connections--;
// 查詢頁面訪問時間
$map = [];
$map[] = ['ip', '=', $connection->getRemoteIp()];
$data = \app\common\model\Tongji::where($map)->findOrEmpty();
if (!$data->isEmpty()) {
$data = $data->toArray();
$time = time();
// 更新統(tǒng)計離開時間
$update = [];
$update['etime'] = $time;
$update['online_time'] = intval($time) - intval($data['stime']);
$where = [];
$where[] = ['ip', '=', $connection->getRemoteIp()];
$where[] = ['visitor_hash', '=', $data['visitor_hash']];
\app\common\model\Tongji::update($update, $where);
\app\common\model\TongjiSession::update($update, $where);
}
$json = [];
$json['type'] = 'close';
$json['worker_id'] = $connection->id;
$json['online'] = $total_connections;
$json['ip'] = $connection->getRemoteIp();
$json['time'] = date('Y-m-d H:i:s');
echo (json_encode($json));
echo (PHP_EOL);
};
// 接收消息回調(diào)
$worker->onMessage = function (TcpConnection $connection, $data) {
// 給connection臨時設(shè)置一個lastMessageTime屬性,用來記錄上次收到消息的時間
$connection->lastMessageTime = time();
// 統(tǒng)計在線人數(shù)
global $total_connections;
// 處理消息
$json = [];
$json['type'] = 'message';
$json['data'] = $data;
$json['online'] = $total_connections;
$json['ip'] = $connection->getRemoteIp();
$json['time'] = date('Y-m-d H:i:s');
// 輸出
$connection->send(json_encode($json));
echo (json_encode($json));
echo (PHP_EOL);
};
// 運行worker
Worker::runAll();
}
前端js
var u = parseURL(k_win_u);
// ws
var url = 'ws://' + u['host'] + ':2346';
// WebSocket
var ws = new ReconnectingWebSocket(url);
ws.onopen = function (e) {
console.log('連接成功!');
}
ws.onmessage = function (e) {
storage.setItem('etime', JsToPHPTime());
storage.setItem('diff', parseInt(JsToPHPTime()) - parseInt(loadStartTime));
var dataConfig = storage.getItem(dataTable);
// 獲取服務(wù)器請求數(shù)據(jù)
var data = eval("(" + e.data + ")");
storage.setItem('ip', data.ip);
switch (data.type) {
case 'connect':
GetWxShow(dataConfig.wx_tel, PAGEDATA['page_id'], data.worker_id, data.ip);
break;
case 'workerstart':
GetWxPing(dataConfig.wx_tel, PAGEDATA['page_id'], data.worker_id, data.ip);
break;
default:
break;
}
};
ws.onclose = function (e) {
console.log('連接已關(guān)閉');
};
目前代碼實現(xiàn)了在線人數(shù),但是無法統(tǒng)計每個頁面的,我該怎么寫。
我在后臺文章列表頁中,每個文章,有一個按鈕,是在線人數(shù),點開后,顯示該文章的頁面在線人數(shù)。。
我感覺應(yīng)該可以 進入的時候 把當前的頁面 或者說 文章ID 給傳進來, 然后在連接的地方 跟 global $total_connections; 的處理方式一樣 維護一個 全局的數(shù)組 數(shù)組的鍵名就是頁面ID 鍵值就是 在線人數(shù)了