This commit is contained in:
2024-07-15 12:20:32 +08:00
parent 8132709a36
commit 404aa14e27
2 changed files with 71 additions and 72 deletions

View File

@@ -8,22 +8,85 @@
namespace app\index\controller;
use app\api\logic\AudioRevertLogic;
use React\EventLoop\Factory;
use React\Socket\TcpConnector;
use React\Socket\SecureConnector;
use React\Socket\ConnectionInterface;
use Evenement\EventEmitter;
/**
* @deprecated
*/
class Test extends BasePublicController
{
function test2(){
function test3(){
$obj = new AudioRevertLogic();
$obj->revert(1,2);
dump('<p>==================================================</p>');
$obj->revert_test(1,2);
}
function test2(){
//$obj = new AudioRevertLogic();
//$obj->revert(1,2);
// 配置您的讯飞应用信息
$appId = 'd482af59';
$apiKey = '0d20dab630904ad8676d9075375a1914';
// 创建事件循环
$loop = Factory::create();
// 创建TcpConnector
$tcpConnector = new TcpConnector($loop);
// 使用TcpConnector创建SecureConnector
$secureConnector = new SecureConnector($tcpConnector, $loop);
// 实时语音转写API地址
$url = 'wss://rtasr.xfyun.cn/v1/ws';
// 计算签名
$ts = time();
$baseString = $appId . $ts;
$md5BaseString = md5($baseString);
$signa = base64_encode(hash_hmac('sha1', $md5BaseString, $apiKey, true));
// 手动构建请求参数,确保特殊字符被正确编码
$params = [
'appid' => $appId,
'ts' => $ts,
'signa' => $signa,
'lang' => 'zh-cn',
];
$queryString = http_build_query($params, '', '&', PHP_QUERY_RFC3986);
// 完整的WebSocket请求URL
$requestUrl = $url . '?' . $queryString;
// 连接到WebSocket服务器
$connector = $secureConnector->connect($requestUrl)->then(function (ConnectionInterface $conn) {
echo "Connected to the WebSocket server\n";
// 接收消息
$conn->on('data', function ($data) {
echo "Received: " . $data . "\n";
});
// 连接关闭
$conn->on('close', function () {
echo "Connection closed\n";
});
// 错误处理
$conn->on('error', function ($error) {
echo "Error: " . $error->getMessage() . "\n";
});
}, function ($error) {
// 连接失败处理
echo "Connection error: " . $error->getMessage() . "\n";
});
// 运行事件循环
$loop->run();
}
function test()
{

View File

@@ -3,74 +3,10 @@
require __DIR__ . '/vendor/autoload.php';
use React\EventLoop\Factory;
use React\Socket\Connector;
use React\Socket\TcpConnector;
use React\Socket\SecureConnector;
use React\Socket\ConnectionInterface;
use React\Promise\PromiseInterface;
use React\Stream\DuplexResourceStream;
use React\Stream\ThroughStream;
$loop = Factory::create(); // 创建事件循环
$socketConnector = new Connector($loop);
// 定义一个函数来处理WebSocket连接
function connectToWebSocket($url, LoopInterface $loop): PromiseInterface {
$parts = parse_url($url);
$host = $parts['host'];
$port = isset($parts['port']) ? $parts['port'] : ($parts['scheme'] === 'wss' ? 443 : 80);
$resource = $parts['path'] . (isset($parts['query']) ? '?' . $parts['query'] : '');
$headers = [
'Host' => $host,
'Upgrade' => 'websocket',
'Connection' => 'Upgrade',
'Sec-WebSocket-Key' => base64_encode(random_bytes(16)),
'Sec-WebSocket-Version' => 13,
];
$handshake = "GET $resource HTTP/1.1\r\n";
foreach ($headers as $name => $value) {
$handshake .= "$name: $value\r\n";
}
$handshake .= "\r\n";
return $socketConnector->connect("$host:$port", $loop)
->then(function (ConnectionInterface $conn) use ($handshake, $loop) {
$conn->write($handshake);
$response = new ThroughStream();
$response->on('data', function ($chunk) use (&$response, $conn) {
if (strpos($chunk, "\r\n\r\n") !== false) {
$response->emit('end');
}
});
$conn->pipe($response);
return $response;
})
->then(function (ThroughStream $response) use ($loop) {
$response->on('end', function () use ($loop) {
$loop->nextTick(function () use ($conn) {
$conn->resume();
});
});
return $conn;
});
}
// 连接到WebSocket服务器
connectToWebSocket('ws://8.130.29.83:2700', $loop)
->then(function ($conn) {
$conn->on('data', function ($data) {
echo "Received: $data\n";
});
$conn->write("Hello Server!\n"); // 发送一条消息
})
->otherwise(function ($error) {
echo "Error: " . $error->getMessage() . "\n";
});
$loop->run(); // 开始事件循环
// 配置您的讯飞应用信息
$appId = '您的应用ID';
$apiKey = '您的apiKey';