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连接,甚至需要预先适应才可以连接。
参数配置
'服务器'=> ['主机'=>'0.0.0.0',//监听地址'port'=> 808,//监听端口'mode'=> SWOOLE_PROCESS,//运行模式默认为SWOOLE_PROCESS'sock_type'=> SWOOLE_SOCK_TCP,//袜子类型默认为SWOOLE_SOCK_TCP'options'=> ['pid_file'=> runtime_path()。'swoole.pid','log_file'=> runtime_path()。'swoole.log','daemonize'=> false,//通常,此值应根据您的cpu内核大1〜4倍。'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'=> ['已启用'=> 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默认会在请求地址后加相应的参数
同时,socketio至少在情况下,会认为http://url/http://socket.io/是支持websocket服务的地址。
而在tp-swoole3.0内部已经该地址请求进行了处理
<&#xff1f;phpnamespace think \ swoole \ websocket \ socketio;使用think \ Config;使用think \ COOKIE;使用think \ Request;类控制器{protected $ transports &#61; [&#39;polling&#39;&#xff0c;&#39;websocket&#39;]; 公共功能升级&#xff08;请求$ request&#xff0c;配置$ config&#xff0c;COOKIE $ COOKIE&#xff09;{如果&#xff08;&#xff01;in_array&#xff08;$ request-> param&#xff08;&#39;transport&#39;&#xff09;&#xff0c;$ this-> transports&#xff09;&#xff09;{返回json&#xff08;[&#39;code&#39;&#61;> 0&#xff0c;&#39;message&#39;&#61;>&#39;未知运输&#xff0c;]&#xff0c;400&#xff09;;}如果&#xff08;$ request-> has&#xff08;&#39;sid&#39;&#xff09;&#xff09;{$ response &#61; response&#xff08;&#39;1&#xff1a;6&#39;&#xff09;;}其他{$ sid &#61; base64_encode&#xff08;uniqid&#xff08;&#xff09;&#xff09;;$ payload &#61; json_encode&#xff08;[&#39;sid&#39;&#61;> $ sid&#xff0c;&#39;upgrades&#39;&#61;> [&#39;websocket&#39;]&#xff0c;&#39;pingInterval&#39;&#61;> $ config-> get&#xff08;&#39;swoole.websocket.ping_interval&#39;&#xff09;&#xff0c;&#39;pingTimeout&#39;&#61;> $ config-> get &#xff08;&#39;swoole.websocket.ping_timeout&#39;&#xff09;&#xff0c;]&#xff09;;$ COOKIE-> set&#xff08;&#39;io&#39;&#xff0c;$ sid&#xff09;;$ response &#61; response&#xff08;&#39;97&#xff1a;0&#39;。$ payload。&#39;2:40&#39;&#xff09;;}返回$ response-> contentType&#xff08;&#39;text / plain&#39;&#xff09;;} public function reject&#xff08;请求$ request&#xff09;{返回json&#xff08;[&#39;code&#39;&#61;> 3&#xff0c;&#39;message&#39;&#61;>&#39;错误请求&#39;&#xff0c;]&#xff0c;400&#xff09;;}}
TP6.0&#xff0c;插件注册采用了服务方式进行了注册&#xff0c;可在tp-swoole服务注册文件中查看路由注册信息&#xff0c;如果想自定义链接规则&#xff0c;则可以覆盖该路由。
<&#xff1f;php // &#43; -------------------------------------------- -------------------------- // | ThinkPHP [我们可以考虑一下] // &#43; ------------------------------------- --------------------------------- // | | 版权所有&#xff08;c&#xff09;2006-2018 http://thinkphp.cn保留所有权利.// &#43; ----------------------------- ----------------------------------------- // | | 许可&#xff08;http://www.apache.org/licenses/LICENSE-2.0&#xff09;// &#43; ----------------------------- ----------------------------------------- // | | 作者&#xff1a;yunwuxin <448901948&#64;qq.com> // &#43; ------------------------------------- ---------------------------------命名空间think \ swoole;将Swoole \ Http \ Server用作HttpServer;使用Swoole \ Websocket \ Server作为WebsocketServer;使用think \ App;使用think \ Route;使用think \ swoole \ command \ Server作为ServerCommand;使用think \ swoole \ facade \ Server;使用think \ swoole \ websocket \ socketio \ Controller; 使用think \ swoole \ websocket \ socketio \ Middleware;类服务扩展\ think \ Service {受保护的$ isWebsocket &#61; false; / *** &#64;var HttpServer | WebsocketServer* /受保护的静态$ server; 公共功能寄存器&#xff08;&#xff09;{$ this-> isWebsocket &#61; $ this-> app-> config-> get&#xff08;&#39;swoole.websocket.enabled&#39;&#xff0c;false&#xff09;; $ this-> app-> bind&#xff08;Server :: class&#xff0c;function&#xff08;&#xff09;{if&#xff08;is_null&#xff08;static :: $ server&#xff09;&#xff09;{$ this-> createSwooleServer&#xff08;&#xff09;;}返回static :: $ server;}&#xff09;; $ this-> app-> bind&#xff08;&#39;swoole.server&#39;&#xff0c;Server :: class&#xff09;; $ this-> app-> bind&#xff08;Swoole :: class&#xff0c;function&#xff08;App $ app&#xff09;{返回新的Swoole&#xff08;$ app&#xff09;;}&#xff09;; $ this-> app-> bind&#xff08;&#39;swoole&#39;&#xff0c;Swoole :: class&#xff09;;}公共功能启动&#xff08;Route $ route&#xff09;{$ this-> commands&#xff08;ServerCommand :: class&#xff09;; 如果&#xff08;$ this-> isWebsocket&#xff09;{$ route-> group&#xff08;function&#xff08;&#xff09;use&#xff08;$ route&#xff09;{$ route-> get&#xff08;&#39;socket.io/&#39;&#xff0c;&#39;&#64;upgrade&#39;&#xff09;;$ route-> post&#xff08;&#39;socket.io/&#39;&#xff0c;&#39;&#64;reject&#39;&#xff09;;}&#xff09;->前缀&#xff08;Controller :: class&#xff09;->中间件&#xff08;Middleware :: class&#xff09;;}} / ***创建Swoole服务器。* /受保护的函数createSwooleServer&#xff08;&#xff09;{$ server &#61; $ this-> isWebsocket吗&#xff1f;WebsocketServer :: class&#xff1a;HttpServer :: class;$ config &#61; $ this-> app-> config;$ host &#61; $ config-> get&#xff08;&#39;swoole.server.host&#39;&#xff09;;$ port &#61; $ config-> get&#xff08;&#39;swoole.server.port&#39;&#xff09;;$ socketType &#61; $ config-> get&#xff08;&#39;swoole.server.socket_type&#39;&#xff0c;SWOOLE_SOCK_TCP&#xff09;;$ mode &#61; $ config-> get&#xff08;&#39;swoole.server.mode&#39;&#xff0c;SWOOLE_PROCESS&#xff09;; static :: $ server &#61; new $ server&#xff08;$ host&#xff0c;$ port&#xff0c;$ mode&#xff0c;$ socketType&#xff09;;$ options &#61; $ config-> get&#xff08;&#39;swoole.server.options&#39;&#xff09;; static :: $ server-> set&#xff08;$ options&#xff09;;}}
套接字使用演示
<&#xff01;DOCTYPE html> 标题 title> script> head> const socket &#61; io&#xff08;&#39;http&#xff1a;// localhost&#xff1a;808&#39;&#xff09;;socket.emit&#xff08;“ test”&#xff0c;“您的消息”&#xff09;;socket.on&#xff08;“ test”&#xff0c;function&#xff08;res&#xff09;{console.log&#xff08;res&#xff09;}&#xff09;; script> body> html>
Websocket路由配置方法
在app目录下新建websocket.php文件&#xff0c;其中需要注意&#xff0c;由于使用了反射&#xff0c;闭包参数名称不能随意定义&#xff0c;不然无法注入。第一个参数是websocket&#xff0c;是当前websocket的服务器对象&#xff0c;第二个参数data是客户端发送的数据。其中&#xff0c;socketio发出的第一个参数和Websocket :: on的第一个参数一致&#xff0c;作为事件名称。
<&#xff1f;php / ***作者&#xff1a;Xavier Yang*日期&#xff1a;2019/6/5*电子邮件&#xff1a;499873958&#64;qq.com* /使用\ think \ swoole \ facade \ Websocket;Websocket :: on&#xff08;“ test”&#xff0c;function&#xff08;\ think \ swoole \ Websocket $ websocket&#xff0c;$ data&#xff09;{// var_dump&#xff08;$ class&#xff09;;$ websocket-> emit&#xff08;“ test”&#xff0c;“ asd”&#xff09;;}&#xff09;;Websocket :: on&#xff08;“ test1”&#xff0c;函数&#xff08;$ websocket&#xff0c;$ data&#xff09;{$ websocket-> emit&#xff08;“ test”&#xff0c;“ asd”&#xff09;;}&#xff09;;Websocket :: on&#xff08;“ join”&#xff0c;function&#xff08;\ think \ swoole \ Websocket $ websocket&#xff0c;$ data&#xff09;{$ websocket-> join&#xff08;“ 1”&#xff09;;}&#xff09;;
tp-swoole3.0同时还有许多其他的新功能&#xff0c;这些功能需要大家去摸索尝试。
我也会在接下来的文章中&#xff0c;一起与大家分享我的使用过程。
以上内容希望帮助到大家&#xff0c;很多PHPer在进阶的时候总会遇到一些问题和瓶颈&#xff0c;业务代码写多了没有方向感&#xff0c;不知道该从那里入手去提升&#xff0c;对此我整理了一些资料&#xff0c;包括但不限于&#xff1a;分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6&#xff0c;laravel&#xff0c;YII2&#xff0c;Redis&#xff0c;Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家&#xff0c;需要的可以加入我的官方群点击此处。