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/是支持websocket服务的地址。

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