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