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