Swoole 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中使用

其中 異步毫秒定時器 可以按照規則處理數據,從而提高異步進行數據查詢之類的操作

  1. swoole_timer_after N秒後執行一次
  2. swoole_timer_tick N秒後執行,循環
  3. swoole_timer_clear 取消這個定時器
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章