This commit is contained in:
mkm 2023-10-13 18:17:51 +08:00
parent 92fd78bafd
commit 9c636041f2

View File

@ -21,10 +21,10 @@ class Pusher
private $chatClient; private $chatClient;
private $ttsConfig =[ private $ttsConfig = [
'aue'=>'lame', 'aue' => 'lame',
'sfl'=>1, 'sfl' => 1,
'vcn'=>'xiaoyan' 'vcn' => 'xiaoyan'
]; ];
public function onConnect(TcpConnection $connection) public function onConnect(TcpConnection $connection)
@ -39,50 +39,57 @@ class Pusher
public function onMessage(TcpConnection $connection, $data) public function onMessage(TcpConnection $connection, $data)
{ {
$data=json_decode($data,true); $data = json_decode($data, true);
// $tts_type=0; $tts_type = 0;
// if(isset($data['tts'])){ if (isset($data['tts'])) {
// $tts_type = $data['tts']; $tts_type = $data['tts'];
// } }
if(isset($data['data'])){ if (isset($data['data'])) {
$data = $data['data']; $data = $data['data'];
} }
if(isset($data[0])&&$data[0]['content']==''){ if (isset($data[0]) && $data[0]['content'] == '') {
return $connection->send(['header'=>['code'=>10003,'message'=>'消息不能为空']]); return $connection->send(['header' => ['code' => 10003, 'message' => '消息不能为空']]);
} }
if ($data != '') { if ($data != '') {
$client = new Client($this->assembleAuthUrl('wss://spark-api.xf-yun.com/v2.1/chat')); $client = new Client($this->assembleAuthUrl('wss://spark-api.xf-yun.com/v2.1/chat'));
// 连接到 WebSocket 服务器 // 连接到 WebSocket 服务器
if ($client) { if ($client) {
$this->chatClient=$client; $this->chatClient = $client;
// 发送数据到 WebSocket 服务器 // 发送数据到 WebSocket 服务器
$data = $this->getBody($this->app_id, $data); $data = $this->getBody($this->app_id, $data);
$client->send($data); $client->send($data);
$text = '';
// 从 WebSocket 服务器接收数据 // 从 WebSocket 服务器接收数据
while (true) { while (true) {
$response = $client->receive(); $response = $client->receive();
$resp = json_decode($response, true); $resp = json_decode($response, true);
$code = $resp["header"]["code"]; $code = $resp["header"]["code"];
// echo "从服务器接收到的数据: " . $response; // echo "从服务器接收到的数据: " . $response;
if(0 == $code){ if (0 == $code) {
$status = $resp["header"]["status"]; $status = $resp["header"]["status"];
if($status != 2){ $arr = explode('。', $resp['payload']['choices']['text'][0]['content']);
// if($tts_type==1){ if ($status != 2) {
// $url=$this->tts($resp['payload']['choices']['text'][0]['content']); if ($tts_type == 1) {
// $resp['payload']['choices']['mp3']=$url; if (count($arr) == 2) {
// $response=json_encode($resp,true); $resp['payload']['choices']['tts_text'] = $text.$arr[0];
$text =$arr[1]??'';
// } $response = json_encode($resp, true);
} else {
$resp['payload']['choices']['tts_text'] = '';
$text .= $resp['payload']['choices']['text'][0]['content'];
$response = json_encode($resp, true);
}
}
$connection->send($response); $connection->send($response);
}else{ } else {
// if($tts_type==1){ if (strlen($text) > 1) {
// $url=$this->tts($resp['payload']['choices']['text'][0]['content']); $resp['payload']['choices']['tts_text'] = $text;
// $resp['payload']['choices']['mp3']=$url; $text = '';
// $response=json_encode($resp,true); $response = json_encode($resp, true);
}
// }
$connection->send($response); $connection->send($response);
// $total_tokens = $resp['payload']['usage']['text']['total_tokens']; // $total_tokens = $resp['payload']['usage']['text']['total_tokens'];
@ -91,8 +98,8 @@ class Pusher
break; break;
} }
} }
if($code==10004){ if ($code == 10004) {
return $connection->send(['header'=>['code'=>10004,'message'=>'上下文超出限制,请清空后重试']]); return $connection->send(['header' => ['code' => 10004, 'message' => '上下文超出限制,请清空后重试']]);
} }
} }
} }
@ -101,7 +108,7 @@ class Pusher
public function onClose(TcpConnection $connection) public function onClose(TcpConnection $connection)
{ {
if($this->chatClient){ if ($this->chatClient) {
$this->chatClient->close(); $this->chatClient->close();
} }
echo "onClose\n"; echo "onClose\n";
@ -177,11 +184,11 @@ class Pusher
return $authAddr; return $authAddr;
} }
function tts($text){ function tts($text)
$name=time().rand(1,100000).'.mp3'; {
$tts=new extend\IFlytek\Xfyun\Speech\TtsClient($this->app_id,$this->api_key,$this->api_secret,$this->ttsConfig); $name = time() . rand(1, 100000) . '.mp3';
file_put_contents(public_path('tts').'/'.$name,$tts->request($text)->getBody()->getContents()); $tts = new extend\IFlytek\Xfyun\Speech\TtsClient($this->app_id, $this->api_key, $this->api_secret, $this->ttsConfig);
return 'https://chat.lihaink.cn/tts/'.$name; file_put_contents(public_path('tts') . '/' . $name, $tts->request($text)->getBody()->getContents());
return 'https://chat.lihaink.cn/tts/' . $name;
} }
} }