thinkphp 6.0 swoole扩展websocket使用教程(life)

这篇教程介绍了在ThinkPHP 6.0中如何利用新的think-swoole 3.0扩展来实现websocket连接,强调了与之前的2.0版本的区别,特别是socketio的配置和使用。文章提到了Websocket基于Http的初始连接机制,以及在tp-swoole中对socketio请求的处理。读者需要对socketio有一定基础。在TP6.0中,websocket路由配置和事件处理进行了详细说明,鼓励读者自行探索更多新功能。
摘要由CSDN通过智能技术生成

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

介绍

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

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

参数配置

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">'服务器'=> ['主机'=>'0.0.0.0',//监听地址</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        'port'=> 808,//监听端口</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        'mode'=> SWOOLE_PROCESS,//运行模式默认为SWOOLE_PROCESS</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        'sock_type'=> SWOOLE_SOCK_TCP,//袜子类型默认为SWOOLE_SOCK_TCP</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        'options'=> ['pid_file'=> runtime_path()。</font><font style="vertical-align: inherit;">'swoole.pid','log_file'=> runtime_path()。</font><font style="vertical-align: inherit;">'swoole.log','daemonize'=> false,//通常,此值应根据您的cpu内核大1〜4倍。</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

            'reactor_num'=> swoole_cpu_num(),'worker_num'=> swoole_cpu_num(),'task_worker_num'=> 4,// swoole_cpu_num(),</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

            '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,</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        ],</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

    ],'websocket'=> ['已启用'=> true,//开启websocket</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        'handler'=> Handler :: class,//自定义wbesocket绑定类</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        'parser'=> Parser :: class,//自定义解析类</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        'route_file'=> base_path()。</font><font style="vertical-align: inherit;">'websocket.php','ping_interval'=> 25000,'ping_timeout'=> 60000,'room'=> [''type'=> TableRoom :: class,'room_rows'=> 4096,'room_size'=> 2048, 'client_rows'=> 8192,'client_size'=> 2048,</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

        ],</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

    ],'auto_reload'=> true,'enable_coroutine'=> true,'resetters'=> [],'tables'=> [],</font></font>

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

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

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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值