异步这种直接用webman自定义进程处理,为何我直接做好后游览器上输入http://127.0.0.1:1234/?device_id=1没反应

lianggan

异步这种直接用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
![](/upload/img/20220224/2462174ab8469c.png)

924 2 0
2个回答

nitron

http内无返回很正常 onMessage内只处理了WebSocket,没有Http Response

xiuwang

ws接口没返回数据就没响应啊,你不是在ws收到数据后才返回http数据么?

年代过于久远,无法发表回答
×
🔝