搜索
首页php框架ThinkPHPthinkphp 6.0 swoole扩展websocket使用教程

thinkphp 6.0 swoole扩展websocket使用教程

前言

ThinkPHP即将迎来最新版本6.0,针对目前越来越流行Swoole,thinkphp也推出了最新的扩展think-swoole 3.0。

介绍

即将推出的tp6.0,已经适配swoole.并推出think-swoole 3.0,并且默认适配了socketio。和2.0版本在使用方法上面有些许不同。

Websocket 继承与Http,进行websocket连接之前需要一次HTTP请求,如果当期地址支持websocket则返回101,然后进行连接。也就是说并不是我的服务支持websocket后,请求每个连接地址都可以进行websocket连接,而是需要预先适配才可以连接。

参数配置

'server'           => [        'host'      => '0.0.0.0', // 监听地址
        'port'      => 808, // 监听端口
        'mode'      => SWOOLE_PROCESS, // 运行模式 默认为SWOOLE_PROCESS
        'sock_type' => SWOOLE_SOCK_TCP, // sock type 默认为SWOOLE_SOCK_TCP
        'options'   => [            'pid_file'              => runtime_path() . 'swoole.pid',            'log_file'              => runtime_path() . 'swoole.log',            'daemonize'             => false,            // Normally this value should be 1~4 times larger according to your cpu cores.
            'reactor_num'           => swoole_cpu_num(),            'worker_num'            => swoole_cpu_num(),            'task_worker_num'       => 4,//swoole_cpu_num(),
            'enable_static_handler' => true,            'document_root'         => root_path('public'),            'package_max_length'    => 20 * 1024 * 1024,            'buffer_output_size'    => 10 * 1024 * 1024,            'socket_buffer_size'    => 128 * 1024 * 1024,            'max_request'           => 3000,            'send_yield'            => true,
        ],
    ],    'websocket'        => [        'enabled'       => true,// 开启websocket
        'handler'       => Handler::class,  //自定义wbesocket绑定类
        'parser'        => Parser::class, //自定义解析类
        'route_file'    => base_path() . 'websocket.php',        'ping_interval' => 25000,        'ping_timeout'  => 60000,        'room'          => [            'type'        => TableRoom::class,            'room_rows'   => 4096,            'room_size'   => 2048,            'client_rows' => 8192,            'client_size' => 2048,
        ],
    ],    'auto_reload'      => true,    'enable_coroutine' => true,    'resetters'        => [],    'tables'           => [],

handler和parser大大方便了自定义websocket服务,默认系统集成socketio。

本文主要介绍如何使用socketio,这里假设大家有socketio有一定了解和使用基础。

socketIo默认会在请求地址后加相应的参数

2919595045-5d172b3528726_articlex.png

同时,socketio默认情况下,会认为 http://url/socket.io/ 是支持websocket服务的地址。

4008665166-5d172b4bacbed_articlex.png

而在tp-swoole3.0内部已经对该地址请求进行了处理

<?phpnamespace think\swoole\websocket\socketio;use think\Config;use think\Cookie;use think\Request;class Controller{    protected $transports = [&#39;polling&#39;, &#39;websocket&#39;];    public function upgrade(Request $request, Config $config, Cookie $cookie)
    {        if (!in_array($request->param('transport'), $this->transports)) {            return json(
                [                    'code'    => 0,                    'message' => 'Transport unknown',
                ],                400
            );
        }        if ($request->has('sid')) {
            $response = response('1:6');
        } else {
            $sid     = base64_encode(uniqid());
            $payload = json_encode(
                [                    'sid'          => $sid,                    'upgrades'     => ['websocket'],                    'pingInterval' => $config->get('swoole.websocket.ping_interval'),                    'pingTimeout'  => $config->get('swoole.websocket.ping_timeout'),
                ]
            );
            $cookie->set('io', $sid);
            $response = response('97:0' . $payload . '2:40');
        }        return $response->contentType('text/plain');
    }    public function reject(Request $request)
    {        return json(
            [                'code'    => 3,                'message' => 'Bad request',
            ],            400
        );
    }
}

TP6.0,插件注册采用了service方式进行了注册,可在tp-swoole 服务注册文件中查看路由注册信息,如果想自定义链接规则,则可以覆盖该路由。

<?php// +----------------------------------------------------------------------// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]// +----------------------------------------------------------------------// | Copyright (c) 2006-2018 http://thinkphp.cn All rights reserved.// +----------------------------------------------------------------------// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )// +----------------------------------------------------------------------// | Author: yunwuxin <448901948@qq.com>// +----------------------------------------------------------------------namespace think\swoole;use Swoole\Http\Server as HttpServer;use Swoole\Websocket\Server as WebsocketServer;use think\App;use think\Route;use think\swoole\command\Server as ServerCommand;use think\swoole\facade\Server;use think\swoole\websocket\socketio\Controller;use think\swoole\websocket\socketio\Middleware;class Service extends \think\Service{    protected $isWebsocket = false;    /**
     * @var HttpServer | WebsocketServer
     */
    protected static $server;    public function register()
    {        $this->isWebsocket = $this->app->config->get('swoole.websocket.enabled', false);        $this->app->bind(Server::class, function () {            if (is_null(static::$server)) {                $this->createSwooleServer();
            }            return static::$server;
        });        $this->app->bind('swoole.server', Server::class);        $this->app->bind(Swoole::class, function (App $app) {            return new Swoole($app);
        });        $this->app->bind('swoole', Swoole::class);
    }    public function boot(Route $route)
    {        $this->commands(ServerCommand::class);        if ($this->isWebsocket) {
            $route->group(function () use ($route) {
                $route->get('socket.io/', '@upgrade');
                $route->post('socket.io/', '@reject');
            })->prefix(Controller::class)->middleware(Middleware::class);
        }
    }    /**
     * Create swoole server.
     */
    protected function createSwooleServer()
    {
        $server     = $this->isWebsocket ? WebsocketServer::class : HttpServer::class;
        $config     = $this->app->config;
        $host       = $config->get('swoole.server.host');
        $port       = $config->get('swoole.server.port');
        $socketType = $config->get('swoole.server.socket_type', SWOOLE_SOCK_TCP);
        $mode       = $config->get('swoole.server.mode', SWOOLE_PROCESS);        static::$server = new $server($host, $port, $mode, $socketType);
        $options = $config->get('swoole.server.options');        static::$server->set($options);
    }
}

Socketio默认使用demo

<!DOCTYPE html><html lang="en"><head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="./static/js/socket.io.js"></script></head><body><script>
    const socket = io('http://localhost:808');
    socket.emit("test", "your message");
    socket.on("test",function(res){console.log(res)});</script></body></html>

Websocket路由配置方法

在app目录下新建websocket.php文件,其中需要注意,由于使用了反射,闭包参数名称不能随意定义,不然无法注入。第一个参数是websocket,是当前websocket的Server对象,第二个参数data是客户端发送的数据。其中socketio emit的第一个参数和Websocket::on的第一个参数一致,作为事件名称。

<?php/**
 * Author:Xavier Yang
 * Date:2019/6/5
 * Email:499873958@qq.com
 */use \think\swoole\facade\Websocket;
Websocket::on("test", function (\think\swoole\Websocket $websocket, $data) {    //var_dump($class);
    $websocket->emit("test", "asd");
});
Websocket::on("test1", function ($websocket, $data) {
    $websocket->emit("test", "asd");
});
Websocket::on("join", function (\think\swoole\Websocket $websocket, $data) {
    $websocket->join("1");
});

2128369977-5d172fd1aa6af_articlex.gif

参考如上方法即可使用全新的websocket服务。当然tp-swoole3.0同样还有许多其他的新功能,这些功能需要大家去摸索尝试。
我也会在接下来的文章中,一起与大家分享我的使用过程。

更多ThinkPHP相关技术文章,请访问ThinkPHP使用教程栏目进行学习!

以上是thinkphp 6.0 swoole扩展websocket使用教程的详细内容。更多信息请关注PHP中文网其他相关文章!

声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

SublimeText3 英文版

SublimeText3 英文版

推荐:为Win版本,支持代码提示!

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

mPDF

mPDF

mPDF是一个PHP库,可以从UTF-8编码的HTML生成PDF文件。原作者Ian Back编写mPDF以从他的网站上“即时”输出PDF文件,并处理不同的语言。与原始脚本如HTML2FPDF相比,它的速度较慢,并且在使用Unicode字体时生成的文件较大,但支持CSS样式等,并进行了大量增强。支持几乎所有语言,包括RTL(阿拉伯语和希伯来语)和CJK(中日韩)。支持嵌套的块级元素(如P、DIV),

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境