Swoole WebSocket服務端與用戶端 持續更新
- 簡單WebSocket 服務端實作
- 簡單WebSocket 用戶端實作
- 通路
- 優化成面向對象
簡單WebSocket 服務端實作
server.php
<?php
$server = new Swoole\WebSocket\Server("0.0.0.0", 9906);
//$server->set([
// 'enable_static_handler' => true, // 開啟靜态資源存在優先通路
// 'document_root' => './', // 靜态資源目錄
//]);
// 監聽WebSocket連結打開事件
$server->on('open', 'onOpen');
function onOpen($server,$request){
print_r($request->fd);
}
// 監聽WebSocket消息事件
$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
$server->push($frame->fd, "this is server");
});
$server->on('close', function ($ser, $fd) {
echo "client {$fd} closed\n";
});
$server->start();
更多方法詳見官網
簡單WebSocket 用戶端實作
client.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>WebSocket測試</title>
</head>
<body>
<h1>WebSocket測試</h1>
<script>
var wsUrl = "ws://127.0.0.1:9906";
var webSocket = new WebSocket(wsUrl);
// 執行個體對象的onOpen屬性
webSocket.onopen = function (e) {
webSocket.send("hello!");
console.log("打開連結成功!")
};
// 執行個體化 onMessage
webSocket.onmessage = function (e) {
console.log("接收的内容:",e.data);
};
// 執行個體化 onClose
webSocket.onclose = function (e) {
console.log("關閉連結");
};
// 執行個體化 onClose
webSocket.onerror = function (e) {
console.log("錯誤:",e.data);
};
</script>
</body>
</html>
通路
可以直接打開這個html頁面 也可以直接在浏覽器運作http://127.0.0.1:9906 不過前提是要打開服務端set的内容 并且能生效(因為筆者的不生效,是以選擇第一種方法)
優化成面向對象
Ws.php
<?php
/**
* Created by PhpStorm.
* User: dom
* Date: 19-2-21
* Time: 上午11:51
*/
class Ws
{
public $ws = NULL;
public function __construct($host, $port)
{
$this->ws = new swoole_websocket_server($host, $port);
$this->ws->set(array(
// 'worker_num' => 8,
// 'max_request' => 10000,
// 'max_conn' => 100000,
// 'dispatch_mode' => 2,
// 'debug_mode' => 0,
// 'daemonize' => false,
'task_worker_num' => 8,
'log_level' => SWOOLE_LOG_ERROR, // 日志等級 關閉開啟debug
'trace_flags' => SWOOLE_TRACE_SERVER, // 日志等級 關閉開啟debug
));
$this->ws->on("open", [$this, 'onOpen']);
$this->ws->on("message", [$this, 'onMessage']);
$this->ws->on("task", [$this, 'onTask']);
$this->ws->on("finish", [$this, 'onFinish']);
$this->ws->on("close", [$this, 'onClose']);
$this->ws->start();
}
/**
* 監聽ws連接配接事件
* @author: ZhuBN
* Date: 19-2-21 上午11:59
*
* @param swoole_websocket_server $ws
* @param $request
*/
public function onOpen(swoole_websocket_server $ws, $request)
{
var_dump($request->fd);
// TODO 執行異步定時器 每隔2秒就列印
// if ($request->fd == 1) {
// $timer = swoole_timer_tick(2000, function ($timerId) {
// echo "執行定時器";
// });
// swoole_timer_clear($timer);
// }
}
/**
* 監聽ws消息事件
* @author: ZhuBN
* Date: 19-2-21 上午11:58
*
* @param swoole_websocket_server $ws
* @param $frame
*/
public function onMessage(swoole_websocket_server $ws, $frame)
{
echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
// TODO task 異步任務調用
// $data = [
// 'task' => 1,
// 'fd' => $frame->fd
// ];
// $ws->task($data);
// TODO 執行異步定時器 5秒後執行一次
// swoole_timer_after(5000,function () use($ws,$frame){
// $ws->push($frame->fd, "定時器給你發送資訊啦!");
// });
$ws->push($frame->fd, "this is server");
}
/**
* 異步任務處理邏輯
* @author: ZhuBN
* Date: 19-2-21 下午2:14
*
* @param swoole_websocket_server $ws
* @param $taskId
* @param $workerId
* @param $data
*
* @return string
*/
public function onTask(swoole_websocket_server $ws, $taskId, $fromId, $data)
{
echo "--------Task---------\n";
print_r($data);
echo "---------------------\n";
sleep(5);
return $data;
}
/**
* 異步任務處理完成回調
* @author: ZhuBN
* Date: 19-2-21 下午2:18
*
* @param swoole_websocket_server $ws
* @param $taskId
* @param $returnData
*/
public function onFinish(swoole_websocket_server $ws, $taskId, $returnData)
{
echo "-------Finish--------\n";
echo "taskId:{$taskId}\n";
print_r($returnData);
echo "---------------------\n";
}
/**
* 登出
* @author: ZhuBN
* Date: 19-2-21 下午2:30
*
* @param swoole_websocket_server $ws
* @param $fd
*/
public function onClose(swoole_websocket_server $ws, $fd)
{
echo "client {$fd} closed\n";
}
}
$obj = new Ws('0.0.0.0', 9906);
其中 task 異步任務處理 可以傳回給使用者資訊,并且同時執行任務,進而提高使用者體驗
task不隻是可以在WebSocket中使用也可以在HttpServer、TCPServer中使用
其中 異步毫秒定時器 可以按照規則處理資料,進而提高異步進行資料查詢之類的操作
- swoole_timer_after N秒後執行一次
- swoole_timer_tick N秒後執行,循環
- swoole_timer_clear 取消這個定時器