Swoole WebSocket服务使用Task任务详解 (面向对象方法)

Swoole WebSocket服务使用Task任务详解 (面向对象方法)

  • 作者: 绝爱七八
  • 博客: https://blog.csdn.net/qq_40193451
  • 日期: 2018.8.24
  • 转载请注明来自绝爱七八的CSDN博客

创建基于swoole的websocket服务**

websoc服务端代码块

如下是使用面向对象方法封装的websocket类:

@requires_authorization
 

/**
 * WebSocket Server
 */
class WebSocket
{
     
    public $server = null;
    private $host = '0.0.0.0';
    private $port = 9505;
    function __construct()
    {
     
        $this->server = new swoole_websocket_server($this->host,$this->port);

        //设置参数
        $this->server->set([
            'document_root' => '/var/www/swoole/html',
            'enable_static_handler' => true,
            'worker_num' => 2,
            'task_worker_num' => 4,
        ]);

        $this->server->on('open', [$this,'onOpen']);
        $this->server->on('message', [$this,'onMessage']);
        $this->server->on('task', [$this,'onTask']);
        $this->server->on('finish', [$this,'onFinish']);
        $this->server->on('close', [$this,'onClose']);

        $this->server->start();
    }


    public function onOpen($server,$request)
    {
     
        var_dump($request->fd);
    }

    //form_id线程ID
    public function onTask($server, $task_id, $from_id, $data)
    {
     
        print_r($data);
        sleep(10);
        return 'on task finish'; //调用finish 或者return告诉线程
    }

    public function onFinish($server, $task_id, $data)
    {
     
        echo 'finish-task-id-'.$task_id;
        echo 'finish-success'.$data;
        // $server->finish($data);
    }

    // $frame 是swoole_websocket_frame对象,包含了客户端发来的数据帧信息
    // $frame->fd,客户端连接的唯一标识socket id,使用$server->push推送数据时需要用到
    // $frame->data,数据内容,可以是文本内容也可以是二进制数据,可以通过opcode的值来判断
    // $frame->opcode,WebSocket的OpCode类型,可以参考WebSocket协议标准文档
    // $frame->finish, 表示数据帧是否完整,一个WebSocket请求可能会分成多个数据帧进行发送(底层已经实现了自动合并数据帧,现在不用担心接收到的数据帧不完整)
    public function onMessage($server,$frame)
    {
     
        echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";

        $data = [
            'fd' => $frame->fd,
            'task' => 1
        ];
        $this->server->task($data);
        $this->server->push($frame->fd, "{$frame->data}");
    }

    public function onClose($ser, $fd)
    {
     
        echo "client {$fd} closed\n";
    }
}

$server = new WebSocket();

客户端代码块

如下是静态ws_client.html页面中使用js 实例化 WebSocket:


<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>swoole websocket clienttitle>
head>
<body>
    <h1>websocket testh1>
    <script>
        var wsUrl = "ws://lining.swoole.com:9505";

        var webSocket = new WebSocket(wsUrl);

        webSocket.onopen = function(evt){
      
            webSocket.send('hello china!');
            console.log("connected-swoole-success");
        }

        webSocket.onmessage = function(evt) {
      
            console.log("connected-swoole-success-"+evt.data);
        }

        webSocket.onclose = function(evt) {
      
            console.log("WebSocketClosed!");
        }

        webSocket.onerror = function(evt) {
       console.log("WebSocketError"); };
    script>
body>
html>

测试步骤

  • 第一步启动服务 php websocket_server.php,成功如下

    Swoole WebSocket服务使用Task任务详解 (面向对象方法)_第1张图片

  • 第二步浏览器访问静态资源(http://127.0.0.1:9505/ws_client.html)

  • 此时控制台会返回如下结果代表成功
    这里写图片描述

connected-swoole-success 代表连接成功
connected-swoole-success-hello china! 服务端返回的消息

  • 此时服务端返回结果如下代表成功
    Swoole WebSocket服务使用Task任务详解 (面向对象方法)_第2张图片
    注解:这里的最后一句话是10s之后返回.

  • 此上是个人初学swoole的一些分享和心得.也是第一次使用csdn做个人博客技术分享,不喜勿喷.谢谢

  • 如果描述不清楚的方法可以评论.评论必回.共同进步,共同成长

你可能感兴趣的:(PHP扩展,swoole,PHP,PHP扩展,超神之路)