ThinkPHP6.0+Swoole连接Websock

由于Swoole不支持windows环境,所以你无法在windows环境下测试,只能使用虚拟机或者WSL环境测试。

我的环境Centos7+PHP8

安装稳TP6定版

composer create-project topthink/think tp

PHP8 安装swoole扩展(参考swoole官网)

安装think-swoole

composer require topthink/think-swoole

修改配置文件config/swoole.php

<?php

use think\swoole\websocket\socketio\Handler;

return [
    'http'       => [
        'enable'     => true,
        'host'       => '0.0.0.0',
        'port'       => 9501,//修改端口号
        'worker_num' => swoole_cpu_num(),
        'options'    => [],
    ],
    'websocket'  => [
        'enable'        => true,//改为true
        'handler'       => Handler::class,
        'ping_interval' => 25000,
        'ping_timeout'  => 60000,
        'room'          => [
            'type'  => 'table',
            'table' => [
                'room_rows'   => 8192,
                'room_size'   => 2048,
                'client_rows' => 4096,
                'client_size' => 2048,
            ],
            'redis' => [//配置redis
                        'host'          => '127.0.0.1',
                        'port'          => 6379,
                        'max_active'    => 3,
                        'max_wait_time' => 5,
            ],
        ],
        'listen'        => [],
        'subscribe'     => [
            app\swoole\controller\Swoole::class//websock类
        ],
    ],
    'rpc'        => [
        'server' => [
            'enable'     => false,
            'host'       => '0.0.0.0',
            'port'       => 9000,
            'worker_num' => swoole_cpu_num(),
            'services'   => [],
        ],
        'client' => [],
    ],
    //队列
    'queue'      => [
        'enable'  => false,
        'workers' => [],
    ],
    'hot_update' => [
        'enable'  => true,
        'name'    => ['*.php'],
        'include' => [app_path()],
        'exclude' => [],
    ],
    //连接池
    'pool'       => [
        'db'    => [
            'enable'        => true,
            'max_active'    => 3,
            'max_wait_time' => 5,
        ],
        'cache' => [
            'enable'        => true,
            'max_active'    => 3,
            'max_wait_time' => 5,
        ],
        //自定义连接池
    ],
    'tables'     => [],
    //每个worker里需要预加载以共用的实例
    'concretes'  => [],
    //重置器
    'resetters'  => [],
    //每次请求前需要清空的实例
    'instances'  => [],
    //每次请求前需要重新执行的服务
    'services'   => [],
];

新建websock类

app/swoole/controller/Swoole.php

<?php
/**
 * Created by PhpStorm.
 * User: Pasa吴
 * Date: 2022/5/29
 * Time: 16:15
 */

namespace app\swoole\controller;


class Swoole
{
    public $ws;

    public function __construct(\think\Container $c)
    {
        $this->ws = $c->make(\think\swoole\Websocket::class);
    }

    //建立连接时回调函数
    public function onConnect()
    {
        $fd = $this->ws->getSender();
        //客户端标识
        //省略给用户绑定fd逻辑......
        echo "用户建立了连接,标识为{$fd}\n";
    }

    public function onClose()
    {
        $fd = $this->ws->getSender();
        echo "标识{$fd}关闭了连接\n";
    }

    public function onMessage($event)
    {
        $fd = $this->ws->getSender();
        var_dump($event);
        echo "服务器:与fd握手成功{$fd}\n";
        $this->ws->emit("这是服务器", $fd);
    }

}

项目根目录执行

php think swoole

在这里插入图片描述

websocket 在线测试工具:http://www.easyswoole.com/wstool.html

在这里插入图片描述

如果连接不上请检查 端口号是否正确,安全组是否开放端口号,swoole是否启动成功

Pasa吴技术博客
请先登录后发表评论
  • latest comments
  • 总共0条评论