foreach中的onMessage怎么对应?

翠滢

<?php

use \Workerman\Worker;
use \Workerman\Connection\AsyncTcpConnection;
use \Workerman\Lib\Timer;

require_once 'Autoloader.php'; 

$worker = new Worker();

// 进程启动时
$worker->onWorkerStart = function()
{
    $members = $db->query("SELECT xxxxxxx");

    foreach ($members as $key => $value) {
        $ws_connection = new AsyncTcpConnection("ws://xxxxx.com/ws/".$value['listenKey']);  

        // 设置以ssl加密方式访问,使之成为wss
        $ws_connection->transport = 'ssl';

        // 连接成功
        $ws_connection->onConnect = function($connection){
        };

        // 远程websocket服务器发来消息时
        $ws_connection->onMessage = function($connection, $data){

        };

        // 连接上发生错误时,一般是连接远程websocket服务器失败错误
        $ws_connection->onError = function($connection, $code, $msg){
            echo "error: $msg\n";
        };

        // 当连接远程websocket服务器的连接断开时
        $ws_connection->onClose = function($connection){
            echo "connection closed\n";
        };

        // 设置好以上各种回调后,执行连接操作
        $ws_connection->connect();
    }

};
?>

我是菜鸟,刚接触workerman。

在foreach中循环出每个member,然后用AsyncTcpConnection来请求ws。
现在遇到的问题是:onMessage是异步回调的,在onMessage内无法获取到外部的$value。这样我就无法知道它返回的$data是对应哪个member的ws。有什么办法能让他们对应上吗?
1172 1 0
1个回答

phpcreeper

function($connection, $data)use($value)

  • 暂无评论
年代过于久远,无法发表回答
×
🔝