$remote_connection->opposite->resumeRecv(); }; // 远程连接发来消息时,进行解密,转发给客户端 $remote_connection->onMessage = function ($remote_connection, $buffer) { $remote_connection->opposite->send($remote_connection->opposite->encryptor->decrypt($buffer)); }; // 远程连接断开时,则断开客户端的连接 $remote_connection->onClose = function ($remote_connection) { // 关闭对端 $remote_connection->opposite->close(); $remote_connection->opposite = null; }; // 远程连接发生错误时(一般是建立连接失败错误),关闭客户端的连接 $remote_connection->onError = function ($remote_connection, $code, $msg) use($address) { echo "remote_connection {$address} error code:{$code} msg:{$msg}\n"; $remote_connection->close(); if ($remote_connection->opposite) { $remote_connection->opposite->close(); } }; // 流量控制 $connection->onBufferFull = function ($connection) { $connection->opposite->pauseRecv(); }; $connection->onBufferDrain = function ($connection) { $connection->opposite->resumeRecv(); }; // 当客户端发来数据时,加密数据,并发给远程服务端 $connection->onMessage = function ($connection, $data) { $connection->opposite->send($connection->encryptor->encrypt($data)); };