swoft icon indicating copy to clipboard operation
swoft copied to clipboard

在http接口里面使用websocket推送消息,app/bean.php里面配置了http响应

Open puj798 opened this issue 5 years ago • 3 comments

/**
     * @RequestMapping("/hi")
     * @return Response
     */
    public function hi():Response
    {

          server()->push(7, 'http响应');

    }

//运行出现Call to undefined method Swoft\Http\Server\HttpServer::push()这个错误

puj798 avatar Feb 02 '21 06:02 puj798

我也有一样的需求,出现了同样的问题,没法在httpServer里面使用wsServer,有什么解决办法了嘛?

guoguicheng avatar Apr 29 '21 08:04 guoguicheng

这个要反过来配置,在ws server里启用 http 就可以了。

inhere avatar Apr 29 '21 11:04 inhere

我在MasterStartListener内判断启动进程为WebSocket时,使用sgo创建一个携程,通过轮询redis,有数据就使用Task发送消息,问题已经解决

public function handle(EventInterface $event): void
{
    /** @var HttpServer $httpServer */
    $httpServer = $event->getTarget();
    $serverType = $httpServer->getServerType();
    if ('WebSocket' === $serverType) {
        sgo(function () {
            Redis::call(function ($redis) {
                while (1) {
                    $data = $redis->rPop('massage');
                    Task::async('msgTask', 'send', [...]);
                }
            });
        });
    }
}

guoguicheng avatar Apr 30 '21 07:04 guoguicheng