thinkphp6.0swoole扩展websocket使用教程(life)

更新时间:2023-05-22 01:53:51 阅读: 评论:0

thinkphp6.0swoole扩展websocket使⽤教程(life)
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 ><font >'服务器'=> ['主机'=>'0.0.0.0',//监听地址</font></font><font></font><font vertical-align: inherit;"><font >
'mode'=> SWOOLE_PROCESS,//运⾏模式默认为SWOOLE_PROCESS</font></font><font></font><font ><font vertical-align: inherit;"><font vertical-align: inherit;">'swoole.pid','log_file'=> runtime_path()。</font><font vertical-align: inherit;"><font >
],'websocket'=> ['已启⽤'=> true,//开启websocket</font></font><font></font><font ><font >
'handler'=> Handler :: class,//⾃定义wbesocket绑定类</font></font><font></font><font ><font >鸡蛋炸酱面的做法
'parr'=> Parr :: class,//⾃定义解析类</font></font><font></font><font ><font >
'route_file'=> ba_path()。</font><font >'websocket.php','ping_interval'=> 25000,'ping_timeout'=> 60000,'room'=> [''typ        ],</font></font><font></font><font ><font >
],'auto_reload'=> true,'enable_coroutine'=> true,'retters'=> [],'tables'=> [],</font></font>
handler和parr⼤⼤⽅便了⾃定义websocket服务,交替系统集成了socketio。
阴符七术
本⽂主要介绍如何使⽤socketio,这⾥假设⼤家有socketio有⼀定了解和使⽤基础。
socketIo默认会在请求地址后加相应的参数
⽽在tp-swoole3.0内部已经该地址请求进⾏了处理
<font ><font ><?phpnamespace think \ swoole \ websocket \ socketio;使⽤think \ Config;使⽤think \ Cook    {如果(!in_array($ request-> param('transport'),$ this-> transports)){返回json(</font></font><font></font><font ><font s                ['code'=> 0,'message'=>'未知运输,</font></font><font></font><font ><font >
],400</font></font><font></font><font ><font >
);</font></font><font></font><font ><font >
}如果($ request-> has('sid')){</font></font><font></font><font ><font >
$ respon = respon('1:6');</font></font><font></font><font ><font >
}其他{</font></font><font></font><font ><font >
$ sid = ba64_encode(uniqid());</font></font><font></font><font ><font >
$ payload = json_encode(</font></font><font></font><font ><font >
['sid'=> $ sid,'upgrades'=> ['websocket'],'pingInterval'=> $ config-> get('swoole.websocket.ping_interval'),'pingTimeout'=> $ config-> get ('swoo                ]</font></font><font></font><font ><font >
);</font></font><font></font><font ><font
>
$ cookie-> t('io',$ sid);</font></font><font></font><font ><font >
$ respon = respon('97:0'。$ payload。'2:40');</font></font><font></font><font ><font >        }返回$ respon-> contentType('text / plain');</font></font><font></font><font ><font >
} public function reject(请求$ request)</font></font><font></font><font ><font >
{返回json(</font></font><font></font><font ><font >
['code'=> 3,'message'=>'错误请求',</font></font><font></font><font ><font >
宝宝积食的症状],400</font></font><font></font><font ><font style="vertical-align: inhe
rit;">
);</font></font><font></font><font ><font >
}</font></font><font></font><font ><font >
}</font></font>
TP6.0,插件注册采⽤了服务⽅式进⾏了注册,可在tp-swoole服务注册⽂件中查看路由注册信息,如果想⾃定义链接规则,则可以覆盖该
路由。
<font ><font ><?php // + -------------------------------------------- -------------------------- // | </font><font vertical-align: inherit;">WebsocketServer</font></font><font></font><font ><font vertical-align: inherit;"><font >
受保护的静态$ rver; </font><font >公共功能寄存器()</font></font><font></font><font ><font style=    {$ this-> isWebsocket = $ this-> app-> config-> get('abled',fal); </font><font >$ this-> app-> bind(Serv            }返回static :: $ rver;</font></font><font></font><font ><font >
}); </font><font >$ this-> app-> bind('swoole.rver',Server :: class); </font><font >$ this-> app        }); </font><font >$ this-> app-> bind('swoole',Swoole :: class);</font></font><font></font><font     }公共功能启动(Route $ route)</font></font><font></font><font ><font >其实我爱你
{$ this-> commands(ServerCommand :: class); </font><font >如果($ this-> isWebsocket){</font></font><font></font><font sty            $ route-> group(function()u($ route){</font></font><font></font><font ><font >
$ route-> get('socket.io/','@upgrade');</font></font><font></font><font ><font >
$ route-> post('socket.io/','@reject');</font></font><font></font><font ><font >
})->前缀(Controller :: class)->中间件(Middleware :: class);</font></font><font></font><font ><font vertical-align: inherit;"><font >
} / **</font></font><font></font><font ><font >
*创建Swoole服务器。</font></font><font></font><font ><font >
* /</font></font><font></font><font ><font >
外婆的歌受保护的函数createSwooleServer()</font></font><font></font><font ><font >
{</font></font><font></font><font ><font >
$ rver = $ this-> isWebsocket吗?</font><font >WebsocketServer :: class:HttpServer :: class;</font></font><font></font><fon        $ config = $ this-> app-> config;</font></font><font></font><font ><font >
$ host = $ config-> get('swoole.rver.host');</font></font><font></font><font ><font >
$ port = $ config-> get('swoole.rver.port');</font></font><font></font><font ><font >
$ socketType = $ config-> get('swoole.rver.socket_type',SWOOLE_SOCK_TCP);</font></font><font></font><font ><font s        $ mode = $ config-> get('de',SWOOLE_PROCESS); </font><font >static :: $ rver = new $ rver($ hos        $ options = $ config-> get('swoole.rver.options'); </font><font >static :: $ rver-> t($ options);</font></font><font></fo    }</font></font><font></font><font ><font >
}</font></font>
套接字使⽤演⽰
<font ><font ><!DOCTYPE html> <html lang =“ zh-CN”> <head></font></font><font></font><font vertical-align: inherit;"><font >
<title>标题</ title></font></font><font></font><font ><font >
<script src =“ ./ static / js / socket.io.js”> </ script> </ head> <body> <script></font></font><font></font><font ><font vertical-align: inherit;"><font >
爵位
<(“ test”,function(res){console.log(res)}); </ script> </ body> </ html></font></font>
Websocket路由配置⽅法
在app⽬录下新建websocket.php⽂件,其中需要注意,由于使⽤了反射,闭包参数名称不能随意定义,不然⽆法注⼊。第⼀个参数是websocket,是当前websocket的服务器对象,第⼆个参数data是客户端发送的数据。其中,socketio发出的第⼀个参数和Websocket ::
on的第⼀个参数⼀致,作为事件名称。
<font ><font ><?php / **</font></font><font></font><font ><font vertical-align: inherit;"><font >
*⽇期:2019/6/5</font></font><font></font><font ><font >
武则天后的皇帝*电⼦邮件:</font></font><font></font><font ><font >
* /使⽤\ think \ swoole \ facade \ Websocket;</font></font><font></font><font ><font >
Websocket :: on(“ test”,function(\ think \ swoole \ Websocket $ websocket,$ data){// var_dum
p($ class);</font></font><font></font><font vertical-align: inherit;"><font >
});</font></font><font></font><font ><font >
Websocket :: on(“ test1”,函数($ websocket,$ data){</font></font><font></font><font ><font >
$ websocket-> emit(“ test”,“ asd”);</font></font><font></font><font ><font >
});</font></font><font></font><font ><font >
Websocket :: on(“ join”,function(\ think \ swoole \ Websocket $ websocket,$ data){</font></font><font></font><font ><font style    $ websocket-> join(“ 1”);</font></font><font></font><font ><font >
});</font></font>
tp-swoole3.0同时还有许多其他的新功能,这些功能需要⼤家去摸索尝试。
我也会在接下来的⽂章中,⼀起与⼤家分享我的使⽤过程。
以上内容希望帮助到⼤家,很多PHPer在进阶的时候总会遇到⼀些问题和瓶颈,业务代码写多了没有⽅向感,不知道该从那⾥⼊⼿去提升,对此我整理了⼀些资料,包括但不限于:分布式架构、⾼可扩展、⾼性能、⾼并发、服务器性能调优、
TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、shell脚本、Docker、微服务、Nginx等多个知识点⾼级进阶⼲货需要的可以免费分享给⼤家,需要的可以加⼊我的官⽅群。列夫托尔斯泰

本文发布于:2023-05-22 01:53:51,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/726458.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:需要   定义   请求   参数   地址   注册   功能
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图