swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.25k stars 3.16k forks source link

请教Swoole既当ws服务端又当ws客户端,转发ws消息的功能实现。 #5314

Closed mdys closed 1 week ago

mdys commented 1 week ago

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error.

需求是 swoole 创建ws服务端, 当客户端 open swoole服务端时候, client创建个ws 客户端连接第三方的ws服务器 ,将客户端的ws消息 转发给第三方ws服务器。同时监听第三方ws的连接状态 和message。 ` //我设想的是通过$ws_clients保存和第三方ws连接的对象,但下面调用这个client状态时候出错了。 protected $ws_clients = [];

$client = new Client('127.0.0.1', 80); $this->ws_clients[$fd]= $client; `

  1. What did you expect to see?

想问如何写法可以实现对第三方服务端状态和消息的监听、消息发送?

当作为服务端时候将接收到的data转发给 之前创建的第三方连接 `public function onMessage( $serv, $frame ) {

$this->ws_clients[$frame->fd]->send($frame->data);

//给ws服务端转发的时候 报错 提示 Uncaught Yurun\Util\YurunHttp\Exception\WebSocketException: Send Failed, error: Websocket handshake failed, errorCode: 8504 /vendor/yurunsoft/yurun-http/src/YurunHttp/WebSocket/Swoole.php on line 138

}` 请教该如何写法可以实现这个 $client 连接状态 和message值的监听?

  1. What did you see instead?

  2. What version of Swoole are you using (show your php --ri swoole)? swoole 5.1.2

  3. What is your machine environment used (show your uname -a & php -v & gcc -v) ? php 8.3.6