异步这种直接用webman自定义进程处理。
新建 process/Api.php
<?php
namespace process;
use Workerman\Protocols\Http\Request;
use Workerman\Connection\AsyncTcpConnection;
class Api
{
public function onMessage($connection, Request $request)
{
$device_id = $request->get('device_id');
$ws = new AsyncTcpConnection('ws://127.0.0.1:2022');
$ws->onMessage = function ($ws, $data) use ($connection) {
$connection->send($data);
$ws->close();
};
$ws->onConnect = function ($ws) use ($device_id) {
$ws->send($device_id);
};
$ws->connect();
}
}
config/process.php中增加配置
return [
// 这里省略了其它配置....
'api' => [
'handler' => \process\Api::class,
'listen' => 'http://0.0.0.0:1234',
]
];
重启webman后访问 http://127.0.0.1:1234/?device_id=1
http内无返回很正常 onMessage内只处理了WebSocket,没有Http Response
弄好了,谢谢
ws接口没返回数据就没响应啊,你不是在ws收到数据后才返回http数据么?
弄好了,谢谢