workerman作为客户端怎么向另一个ws服务端发送emit指令

Bygones

workerman作为客户端怎么向另一个ws服务端发送emit指令

use Workerman\Worker;
use Workerman\Connection\AsyncTcpConnection;
$worker = new Worker();
// 进程启动时
$worker->onWorkerStart = function()
{
    // 以websocket协议连接远程websocket服务器
    $ws_connection = new AsyncTcpConnection("ws://0.0.0.0:9501/socket.io/?EIO=3&transport=websocket");
    // 连上后发送hello字符串
    $ws_connection->onConnect = function($connection){
        $connection->send('hello');
    };
    // 远程websocket服务器发来消息时
    $ws_connection->onMessage = function($connection, $data){
        echo "recv: $data\n";
    };
    // 连接上发生错误时,一般是连接远程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();
};
Worker::runAll();
2913 0 0
0个回答

年代过于久远,无法发表回答
×
🔝