thinkphp 6.0 swoole扩展websocket使用教程

前言

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默认会在请求地址后加相应的参数

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值