前言
thinkphp即将迎来最新版本6.0,针对目前越来越流行swoole,thinkphp也推出了最新的扩展think-swoole 3.0。
介绍
即将推出的tp6.0,已经适配swoole.并推出think-swoole 3.0,并且默认适配了socketio。和2.0版本在使用方法上面有些许不同。
websocket 继承与http,进行websocket连接之前需要一次http请求,如果当期地址支持websocket则返回101,然后进行连接。也就是说并不是我的服务支持websocket后,请求每个连接地址都可以进行websocket连接,而是需要预先适配才可以连接。
参数配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
'rver'
=> [
'host'
=>
'0.0.0.0'
,
// 监听地址
'port'
=> 808,
// 监听端口
'mode'
=> swoole_process,
// 运行模式 默认为swoole_process
'sock_type'
=> swoole_sock_tcp,
// sock type 默认为swoole_sock_tcp
'options'
=> [
'pid_file'
=> runtime_path() .
'swoole.pid'
,
'log_file'
=> runtime_path() .
'swoole.log'
,
'daemonize'
=> fal,
// normally this value should be 1~4 times larger according to your cpu cores.
'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,
'nd_yield'
=> true,
],
],
'websocket'
=> [
'enabled'
=> true,
// 开启websocket
'handler'
=> handler::
class
,
//自定义wbesocket绑定类
'parr'
=> parr::
class
,
//自定义解析类
'route_file'
=> ba_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,
'retters'
=> [],
'tables'
=> [],
handler和parr大大方便了自定义websocket服务,默认系统集成socketio。
本文主要介绍如何使用socketio,这里假设大家有socketio有一定了解和使用基础。
socketio默认会在请求地址后加相应的参数
同时,socketio默认情况下,会认为 http://url/socket.io/ 是支持websocket服务的地址。
而在tp-swoole3.0内部已经对该地址请求进行了处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?phpnamespace think\swoole\websocket\socketio;
u
think\config;
u
think\cookie;
u
think\request;
class
controller{
protected
$transports
= [
'polling'
,
'websocket'
];
public
function
upgrade(request
$request
, config
$config
, cookie
$cookie
)
{
if
(!in_array(
$request
->param(
'transport'
),
$this
->transports)) {
return
json(
[
'code'
=> 0,
'message'
=>
'transport unknown'
,
], 400
韩国电影我的朋友他的妻子);
}
if
(
$request
->has(
'sid'
)) {
$respon
= respon(
'1:6'
);
}
el
{
$sid
=
ba64_encode
(uniqid());
$payload
= json_encode(
[
'sid'
=>
$sid
,
'upgrades'
=> [
'websocket'
],
'pinginterval'
=>
$config
->get(
'swoole.websocket.ping_interval'
),
'pingtimeout'
=>
$config
->get(
'swoole.websocket.ping_timeout'
),
]
);
$cookie
->t(
'io'
,
$sid
);
$respon
= respon(
'97:0'
.
$payload
.
'2:40'
);
}
return
$respon
->contenttype(
'text/plain'
);
}
public
function
reject(request
$request
)
{
return
json(
[
'code'
=> 3,
'message'
=>
'bad request'
,
], 400
);
}
}
tp6.0,插件注册采用了rvice方式进行了注册,可在tp-swoole 服务注册文件中查看路由注册信息,如果想自定义链接规则,则可以覆盖该路由。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
// +----------------------------------------------------------------------// | thinkphp [ we can do it just think it ]// +----------------------------------------------------------------------// | copyright (c) 2006-2018 /d/file/titlepic/www.thinkphp.cn all rights rerved.// +----------------------------------------------------------------------// | l情景对话icend ( http://www.apache.org/licens/licen-2.0 )// +----------------------------------------------------------------------// | author: yunwuxin <448901948@qq.com>// +----------------------------------------------------------------------namespace think\swoole;u swoole\http\rver as httprver;u swoole\websocket\rver as w分子式是什么ebsocketrver;u think\app;u think\route;u think\swoole\command\rver as rvercommand;u think\swoole\facade\rver;u think\swoole\websocket\socketio\controller;u think\swoole\websocket\socketio\middleware;class rvice extends \think\rvice{ protected $iswebsocket = fal; /**
* @
var
httprver | websocketrver
*/
protected
static
$rver
;
public
function
registe基尔霍夫定律r()
{
$this
->iswebsocket =
$this
->app->config->get(
'swoole.websocket.enabled'
, fal);
$this
->app->bind(rver::
class
,
function
() {
if
(
is_null
(
static
::
$rver
)) {
$this
->createswoolerver();
}
return
static
::
$rver
;
});
$this
->app->bind(
'swoole.rver'
, rver::
class
);
$this
->app->bind(swoole::
class
,
function
(app
$app
) {
return
new
swoole(
$app
);
});
$this
->app->bind(
'swoole'
, swoole::
class
);
}
public
function
boot(route
$route
)
{
$this
->commands(rvercommand::
class
);
if
(
$this
->iswebsocket) {
$route
->group(
function
()
u
(
$route
) {
$route
->get(
'socket.io/'
,
'@upgrade'
);
$route
->post(
'socket.io/'
,
'@reject'
);
})->prefix(controller::
class
)->middleware(middleware::
class
);
}
}
/**
* create swoole rver.
*/
protected
function
createswoolerver()
{
$rver
=
$this
->iswebsocket ? websocketrver::
class
: httprver::
class
;
$config
=
$this
->app->config;
$host
=
$config
->get(
'swoole.rver.host'
);
$port
=
$config
->get(
'swoole.rver.port'
);
$sockettype
=
$config
->get(
'swoole.rver.socket_type'
, swoole_sock_tcp);
$mode
=
$config
->get(
'swoole.rver.mode'
, swoole_process);
static
::
$rver
=
new
$rver
(
$host
,
$port
,
$mode
,
$sockettype
);
$options
=
$config
->get(
'swoole.rver.options'
);
static
::
$rver
->t(
$options
);
}
}
socketio默认使用demo
1
2
3
4
5
6
7
<!doctype html><html lang=
"en"
><head>
<meta chart=
"utf-8"
>
<title>title</title>
<script src=
"./static/js/socket.io.js"
></script></head><body><script>
const
socket = io(
'http://localhost:808'
);
socket.emit(
"test"
,
"your message"
);
socket.on(
"test"
,
function
(res){console.log(res)});</script></body></html>
websocket路由配置方法
在app目录下新建websocket.php文件,其中需要注意,由于使用了反射,闭包参数名称不能随意定义,不然无法注入。第一个参数是websocket,是当前websocket的rver对象,第二个参数data是客户端发送的数据。其中socketio emit的第一个参数和websocket::on的第一个参数一致,作为事件名称。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
/**
* author:xavier yang
* date:2019/6/5
* email:499873958@qq.com
*/
u
\think\swoole\facade\websocket;
websocket::on(
"test"
,
function
(\think\swoole\webso凉生大结局cket
$websocket
,
$data
) {
//var_dump($class);
$websocket
->emit(
"test"
,
"asd"
);
});
websocket::on(
"test1"
,
function
(
$websocket
,
$data
) {
$websocket
->emit(
"test"
,
"asd"
);
});
websocket::on(
"join"
,
function
(\think\swoole\websocket
$websocket
,
$data
) {
$websocket
->join(
"1"
);
});
参考如上方法即可使用全新的websocket服务。当然tp-swoole3.0同样还有许多其他的新功能,这些功能需要大家去摸索尝试。
我也会在接下来的文章中,一起与大家分享我的使用过程。
本文发布于:2023-04-07 19:20:33,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/6d4967ee2fb0607d5e8d7c0926e3ed02.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:thinkphp 6.0 swoole扩展websocket使用教程.doc
本文 PDF 下载地址:thinkphp 6.0 swoole扩展websocket使用教程.pdf
留言与评论(共有 0 条评论) |