~~~
创建项目:composer create-project topthink/think=5.1.* tp5
添加swoole扩展:composer require topthink/think-swoole=2.0.* 需要在linux下执行
php think swoole 开启swoole服务
~~~
~~~
接口跨域问题:修改/vendor/topthink/think-swoole/src/Http.php
public function onRequest($request, $response)
{
$response->header('Access-Control-Allow-Origin', $request->header['origin'] ?? '');
$response->header('Access-Control-Allow-Methods', 'GET,POST,PUT,DELETE,OPTIONS,PATCH');
$response->header('Access-Control-Allow-Headers', 'x-requested-with,session_id,Content-Type,token,Origin');
$response->header('Access-Control-Max-Age', '86400');
$response->header('Access-Control-Allow-Credentials', 'true');
if ($request->server['request_method'] == 'OPTIONS') {
$response->status(200);
$response->end();
return;
};
// 执行应用并响应
$this->app->run($request, $response);
}
~~~
~~~
https://www.cnblogs.com/cshaptx4869/p/11367137.html //think-swoole 自定义Onmessage,
Onclose
系统内定义了Message,Close回调,在此配置的是不会执行滴
$server = WebSocketFrame::getInstance()->getServer(); //获取服务
$frame = WebSocketFrame::getInstance()->getFrame(); //获取客户端信息
$data = WebSocketFrame::getInstance()->getData(); //获取数据
~~~
~~~
监听多端口
~~~