??方案一:將服務端當客戶端去鏈接socket服務,給指定客戶端發消息
??方案二:通過http服務去給指定客戶發送消息
#### 根據 `PHPSocket.io`里面提供的方案,這里選擇方案二,用http請求去給指定客戶端發送消息
??調用方式(GET觸發):前提是用戶996跟997都登陸上線了 <a href="http://114.96.100.72:5657/ " style="background-color: rgb(255 82 0); color: #ffffff; border: none; padding: 10px 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; transition-duration: 0.4s; cursor: pointer; border-radius: 4px; box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);">??優先點擊上線 </a> ?? ?? ?? ?? ?? ?? ?? ??
*****
<a href="http://114.96.100.72:2121/?type=publish&content=你好,這是服務端消息&to=996" style="background-color: #007bff; color: #ffffff; border: none; padding: 10px 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; transition-duration: 0.4s; cursor: pointer; border-radius: 4px; box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);">
??后端給用戶996發消息
</a>
<a href="http://114.96.100.72:2121/?type=publish&content=你好,這是服務端消息&to=997" style="background-color: #007bff; color: #ffffff; border: none; padding: 10px 20px; text-align: center; text-decoration: none; display: inline-block; font-size: 16px; transition-duration: 0.4s; cursor: pointer; border-radius: 4px; box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.3);">
?? 后端給用戶997發消息
</a>
<div style="width:100%;margin-top:10px;padding:20px; height: 180px; background-color: #ffffff; border-radius: 10px; box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);">
<style>
.styled-link { color: #007bff; text-decoration: none; transition: color 0.3s ease; } .styled-link:hover { color: #ff0000; }
</style>
<span style="color: #007bff; text-decoration: none; transition: color 0.3s ease;" onmouseover="this.style.color = '#ff0000';" onmouseout="this.style.color = '#007bff';">
http://114.96.100.72:2121/?type=publish&content=你好,這是服務端消息&to=996
</span>
<b>鏈接說明</b>
??content:是內容
??to:是對應的鏈接到socket的用戶id,這個是用戶自定義的。如果放到業務場景里面也可以取數據庫的id
??114.96.100.72:2121:是我部署的一臺測試服務器,后期如果服務器到期了,會換這個地址
</div>
*****
核心代碼:
```
// 當$sender_io啟動后監聽一個http端口,通過這個端口可以給任意uid或者所有uid推送數據
$sender_io->on('workerStart', function(){
// 監聽一個http端口
$inner_http_worker = new Worker('http://0.0.0.0:2121');
// 當http客戶端發來數據時觸發
$inner_http_worker->onMessage = function(TcpConnection $http_connection, Request $request){
global $uidConnectionMap;
$post = $request->post();
$post = $post ? $post : $request->get();
// 推送數據的url格式 type=publish&to=uid&content=xxxx
switch(@$post['type']){
case 'publish':
global $sender_io;
$to = @$post['to'];
$post['content'] = htmlspecialchars(@$post['content']);
// 有指定uid則向uid所在socket組發送數據
if($to){
$sender_io->to($to)->emit('new_msg', $post['content']);
// 否則向所有uid推送數據
}else{
$sender_io->emit('new_msg', @$post['content']);
}
// http接口返回,如果用戶離線socket返回fail
if($to && !isset($uidConnectionMap[$to])){
return $http_connection->send('offline');
}else{
return $http_connection->send('ok');
}
}
return $http_connection->send('fail');
};
// 執行監聽
$inner_http_worker->listen();
// 一個定時器,定時向所有uid推送當前uid在線數及在線頁面數
Timer::add(1, function(){
global $uidConnectionMap, $sender_io, $last_online_count, $last_online_page_count;
$online_count_now = count($uidConnectionMap);
$online_page_count_now = array_sum($uidConnectionMap);
// 只有在客戶端在線數變化了才廣播,減少不必要的客戶端通訊
if($last_online_count != $online_count_now || $last_online_page_count != $online_page_count_now)
{
$sender_io->emit('update_online_count', "當前<b>{$online_count_now}</b>人在線,共打開<b>{$online_page_count_now}</b>個頁面");
$last_online_count = $online_count_now;
$last_online_page_count = $online_page_count_now;
}
});
});
```