~~~
創建項目: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(); //獲取數據
~~~
~~~
監聽多端口
~~~