关于gateway worker 相关问题

萌萌哒

问题描述

我想onWorkerStart方法里面去异步请求一个连接,然后在onMessage里面将异步请求的结果send出去,该怎么做。

436 2 0
2个回答

SillyDog

这么搞的目的是什么 探讨一下 可能有更好的方案

SillyDog
// transmit.php
<?php

use Workerman\Timer;
use Workerman\Worker;
use Workerman\Protocols\Http\Request;
use Workerman\Connection\TcpConnection;
use Workerman\Connection\AsyncTcpConnection;

require_once __DIR__ . '/vendor/autoload.php';

require './transmitHandle.php';

$worker = new Worker('http://0.0.0.0:8080');

$handle = new transmitHandle();

// 调用类的方法
$worker->onWorkerStart      = array($handle, 'onWorkerStart');
$worker->onMessage          = array($handle, 'onMessage');

// $wsCon = null;

// $worker->onWorkerStart = function () use (&$wsCon) {

//     $wsCon = new AsyncTcpConnection('ws://echo.websocket.org:443');

//     // 设置以ssl加密方式访问,使之成为wss
//     $wsCon->transport = 'ssl';
//     // 当收到消息时
//     $wsCon->onMessage = function (AsyncTcpConnection $con, $data) {
//         echo 'message:' . $data . "\n";
//     };
//     Timer::add(3, function () use ($wsCon) {
//         $wsCon->send('hello');
//         echo "send Hello \n";
//     });

//     $wsCon->connect();
// };

// $worker->onMessage = function (TcpConnection $connection, Request $request) use ($wsCon) {
//     $name = $request->get("name", '');
//     // $request为请求对象,这里没有对请求对象执行任何操作直接返回hello给浏览器
//     $connection->send("hello " . $name);
//     if ($name) {
//         $wsCon->send("hello " . $name);
//     }
// };

Worker::runAll();

//transmitHandle.php

<?php

use Workerman\Timer;
use Workerman\Worker;
use Workerman\Protocols\Http\Request;
use Workerman\Connection\TcpConnection;
use Workerman\Connection\AsyncTcpConnection;

class transmitHandle
{
    protected ?\Workerman\Connection\AsyncTcpConnection $wsCon = null;

    public function __construct()
    {
    }
    public function onWorkerStart(Worker $worker)
    {

        Timer::add(1, function () use ($worker) {
            $this->wsCon = new AsyncTcpConnection('ws://echo.websocket.org:443');

            // 设置以ssl加密方式访问,使之成为wss
            $this->wsCon->transport = 'ssl';
            // 当收到消息时
            $this->wsCon->onMessage = function (AsyncTcpConnection $con, $data) {
                echo 'message:' . $data . "\n";
            };
            $this->wsCon->connect();
        }, null, false);

        Timer::add(10, function () {
            $this->wsCon->send('hello');
            echo "send Hello \n";
        });
    }
    public function onMessage(TcpConnection $connection, Request $request)
    {
        $name = $request->get("name", '');
        // $request为请求对象,这里没有对请求对象执行任何操作直接返回hello给浏览器
        $connection->send("hello " . $name);
        if ($name) {
            echo "http send Hello" . $name . " \n";
            $this->wsCon->send("hello " . $name);
        }
    }
}
  • SillyDog 2024-02-21

    我这个是worker man 下 代码差不多

  • 萌萌哒 2024-02-22

    感谢大佬,但是还有个问题,如果进程数量开多个,我发现他会AsyncTcpConnection 连接多次,能否改成进程数量多个,只异步连接一次呢?

  • SillyDog 2024-02-22

    请求进来的塞到队列里边 然后 单独启动一个进程消费 只在消费进程上 连接异步tcp

  • 萌萌哒 2024-02-22

    好的,按照大佬思路,我单起了一个进程异步连接,用mqtt进行通讯,但是我发现mqtt连上两秒,就会报Connection fail 我在mqtt onerror 里面打印错误信息,message:Connection fail . code:102 这是什么问题?

  • SillyDog 2024-02-22

    引入个中间件操作吧 保证稳定 代码逻辑也更传统

  • 萌萌哒 2024-02-22

    感谢大佬

×
🔝