最新消息:自律使人变得更强大

Swoole的WebSocket服务简单实现

admin 267 次 0 条

服务端代码 ws_server.php

<?php

class Ws{

    private $host = '0.0.0.0';
    private $port = 8989;    
    private $ws   = null;

    public function __construct(){
        $this->ws = new Swoole\WebSocket\Server($this->host, $this->port);

        $this->ws->set([
            'woker_num'      => 4,
            'task_woker_num' => 2
        ]);

        $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();
    }

    /**
     * [onOpen description]
     * @Author   WRC
     * @DateTime 2020-06-27T19:50:26+0800
     * @param    [type]                   $ws      [description]
     * @param    [type]                   $request [description]
     * @return   [type]                            [description]
     */
    public function onOpen($ws,$request){
        var_dump($request->fd);
        $ws->push($request->fd,"id:{$request->fd}");
    }

    /**
     * [onMessage description]
     * @Author   WRC
     * @DateTime 2020-06-27T19:52:39+0800
     * @param    [type]                   $ws    [description]
     * @param    [type]                   $frame [description]
     * @return   [type]                          [description]
     */
    public function onMessage($ws,$frame){
        echo "Message: {$frame->data}\n";
        $ws->push($frame->fd, "server: {$frame->data}");
    }

    /**
     * [onTask description]
     * @Author   WRC
     * @DateTime 2020-06-27T20:01:34+0800
     * @param    [type]                   $serv     [description]
     * @param    [type]                   $taskId   [description]
     * @param    [type]                   $workerId [description]
     * @param    [type]                   $data     [description]
     * @return   [type]                             [description]
     */
    public function onTask($serv, $taskId, $workerId, $data){
        print_r($data);
        // 耗时场景 10s
        sleep(10);
        return "on task finish"; // 告诉worker                
    }


    /**
     * [onFinish description]
     * @Author   WRC
     * @DateTime 2020-06-27T20:02:22+0800
     * @param    [type]                   $serv   [description]
     * @param    [type]                   $taskId [description]
     * @param    [type]                   $data   [description]
     * @return   [type]                           [description]
     */
    public function onFinish($serv, $taskId, $data){
        echo "taskId:{$taskId}\n";
        echo "finish-data-sucess:{$data}\n";        
    }

    /**
     * [onClose description]
     * @Author   WRC
     * @DateTime 2020-06-27T19:56:02+0800
     * @param    [type]                   $ws [description]
     * @param    [type]                   $fd [description]
     * @return   [type]                       [description]
     */
    public function onClose($ws,$fd){
        echo "client-{$fd} is closed\n";
    }
}

$obj = new Ws();

客户端代码 ws_client.html

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>

<body>
    <h1>ws_client_test</h1>
</body>
<script type="text/javascript">
    var wsServer = 'ws://192.168.32.129:8989';//这是虚拟机的地址  
    var websocket = new WebSocket(wsServer);
    websocket.onopen = function (evt) {
        websocket.send('hello wsServer');
        console.log("Connected to WebSocket server.");
    };

    websocket.onclose = function (evt) {
        console.log("Disconnected");
    };

    websocket.onmessage = function (evt) {
        console.log('Retrieved data from server: ' + evt.data);
    };

    websocket.onerror = function (evt, e) {
        console.log('Error occured: ' + evt.data);
    };
</script>
</html>

在服务端执行 php ws_server.php 在浏览器访问ws_client.html页面就能看到效果了。192.168.32.129:8989这个是我虚拟机的地址 可以启动一个http_server来访问。

与本文相关的文章

发表我的评论

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址