windows系统下,两个work之间通信问题

加把劲

问题描述

windows系统下,两个work之间通信问题,目前awork和a客户端通信没问题,bwork和b客户端通信没问题
求解答

程序代码

//awork
$ws = new Worker("websocket://0000:2346");
define('Heartbeat',55);
$ws->count = 1;
$ws->uidConnections = array();
$ws->onWorkerStart = function ($ws){
//设置一个 间隔 1秒的定时
    Timer::add(1,function ()use($ws){
        $time_now = time();
        foreach($ws->connections as $connection) {
            if (empty($connection->lastMessageTime)) {
                $connection->lastMessageTime = $time_now;
                continue;
            }
            if ($time_now - $connection->lastMessageTime > Heartbeat) {
                $connection->close();
            }
        }

    });
    $text_worker = new Worker('Text://0000:5656');
    $ws->textWorker = $text_worker;
    $text_worker->listen();
};
$ws->onMessage = function($connection, $data)
{
    global $ws;
    $connection->lastMessageTime = time();
    if(!isset($connection->uid))
    {
        if(intval($data))
        {
            $connection->uid = $data;
            echo $connection->uid."已上线\n";
            $ws->uidConnections[$connection->uid] = $connection;
        }else{
            echo  $connection->uid.$data;
        }
        return ;
    }else{
        $sid= $connection->uid;
        $con = new mysqli($server, $db_username, $db_password,$db_name);
        $stm  = $con->prepare('select m.id 
                from manage m where m.aid=?
                ;');
        $stm->bind_param('s', $sid);
        $stm->execute();
        $result = $stm ->get_result();
        if (mysqli_num_rows($result) >0) {
            while ($row = mysqli_fetch_assoc($result)){
                $mid=$row["id"];
            }
        }
        echo $mid;//这里有数据,下面报错
        global $text_worker;
        $text_worker->onMessage = function($connection, $mid)
        {
            $connection->send($mid);
            echo $mid;
        };
    }
};
//bwork
$ws2->onMessage = function($connection, $data)
{
    global $ws2;
    $connection->lastMessageTime = time();
    if(!isset($connection->mid))
    {
        if(intval($data))
        {
            $connection->mid = $data;
            echo $connection->mid."已上线\n";
            $ws2->midConnections[$connection->mid] = $connection;
        }else{
            echo  $connection->mid.$data;
        }
        return ;
    }else{
        $buffer= $connection->mid;
        echo  $connection->mid.$data;
        $client = stream_socket_client("tcp://0000:5656");
// text 协议为字符串末尾加一个换行符 \n
        echo $data;
        fwrite($client, $data."\n");
    }

};

报错信息

 Attempt to assign property "onMessage" on null i
318 1 0
1个回答

walkor 打赏

$ws2 没定义

  • 加把劲 2024-07-12

    感谢。感觉错误比较多。今天仔细查看了文档。研究gatewaywork中。。。

×
🔝