这里写问题描述
由于外接第三方的WS,他们最多只允许3个ws connect
因此萌生了一个想法:做一个数据转发,异步发一个connect给第三方,然后拿到数据,在转发给多个客户端。
现在出现一个问题:每次客户端连接,我的发现都会异步介入了一次第三方,有没有大佬帮我看下为啥实现不了我想要的效果。
$worker = new Worker('websocket://0.0.0.0:8123');
$worker->onMessage = function (TcpConnection $connection, $data) use ($worker) {
TcpConnection::$defaultMaxSendBufferSize = 1048576000;
// 发起异步连接
$con = new AsyncTcpConnection('ws://ws.xxx.com/');
$con->onConnect = function(AsyncTcpConnection $con) use ($data, $worker){
$con->send('{"action":"subscribe","params":{"data":"111"}}');
};
$con->onMessage = function (AsyncTcpConnection $con, $data) use ($connection,$worker){
foreach($worker->connections as $c)
{
$c->send($data);
}
};
$con->connect();
};