diff --git a/main/protocols/mqtt_protocol.cc b/main/protocols/mqtt_protocol.cc index 4a118778..59984d11 100644 --- a/main/protocols/mqtt_protocol.cc +++ b/main/protocols/mqtt_protocol.cc @@ -100,14 +100,16 @@ bool MqttProtocol::StartMqttClient(bool report_error) { return true; } -void MqttProtocol::SendText(const std::string& text) { +bool MqttProtocol::SendText(const std::string& text) { if (publish_topic_.empty()) { - return; + return false; } if (!mqtt_->Publish(publish_topic_, text)) { ESP_LOGE(TAG, "Failed to publish message: %s", text.c_str()); SetError(Lang::Strings::SERVER_ERROR); + return false; } + return true; } void MqttProtocol::SendAudio(const std::vector& data) { @@ -174,7 +176,9 @@ bool MqttProtocol::OpenAudioChannel() { message += "\"audio_params\":{"; message += "\"format\":\"opus\", \"sample_rate\":16000, \"channels\":1, \"frame_duration\":" + std::to_string(OPUS_FRAME_DURATION_MS); message += "}}"; - SendText(message); + if (!SendText(message)) { + return false; + } // 等待服务器响应 EventBits_t bits = xEventGroupWaitBits(event_group_handle_, MQTT_PROTOCOL_SERVER_HELLO_EVENT, pdTRUE, pdFALSE, pdMS_TO_TICKS(10000)); diff --git a/main/protocols/mqtt_protocol.h b/main/protocols/mqtt_protocol.h index d7253fe6..8c75cd7b 100644 --- a/main/protocols/mqtt_protocol.h +++ b/main/protocols/mqtt_protocol.h @@ -54,7 +54,7 @@ private: void ParseServerHello(const cJSON* root); std::string DecodeHexString(const std::string& hex_string); - void SendText(const std::string& text) override; + bool SendText(const std::string& text) override; }; diff --git a/main/protocols/protocol.h b/main/protocols/protocol.h index c1ef1d47..df7a36b6 100644 --- a/main/protocols/protocol.h +++ b/main/protocols/protocol.h @@ -69,7 +69,7 @@ protected: std::string session_id_; std::chrono::time_point last_incoming_time_; - virtual void SendText(const std::string& text) = 0; + virtual bool SendText(const std::string& text) = 0; virtual void SetError(const std::string& message); virtual bool IsTimeout() const; }; diff --git a/main/protocols/websocket_protocol.cc b/main/protocols/websocket_protocol.cc index e3d25b30..db610933 100644 --- a/main/protocols/websocket_protocol.cc +++ b/main/protocols/websocket_protocol.cc @@ -33,15 +33,18 @@ void WebsocketProtocol::SendAudio(const std::vector& data) { websocket_->Send(data.data(), data.size(), true); } -void WebsocketProtocol::SendText(const std::string& text) { +bool WebsocketProtocol::SendText(const std::string& text) { if (websocket_ == nullptr) { - return; + return false; } if (!websocket_->Send(text)) { ESP_LOGE(TAG, "Failed to send text: %s", text.c_str()); SetError(Lang::Strings::SERVER_ERROR); + return false; } + + return true; } bool WebsocketProtocol::IsAudioChannelOpened() const { @@ -116,7 +119,9 @@ bool WebsocketProtocol::OpenAudioChannel() { message += "\"audio_params\":{"; message += "\"format\":\"opus\", \"sample_rate\":16000, \"channels\":1, \"frame_duration\":" + std::to_string(OPUS_FRAME_DURATION_MS); message += "}}"; - websocket_->Send(message); + if (!SendText(message)) { + return false; + } // Wait for server hello EventBits_t bits = xEventGroupWaitBits(event_group_handle_, WEBSOCKET_PROTOCOL_SERVER_HELLO_EVENT, pdTRUE, pdFALSE, pdMS_TO_TICKS(10000)); diff --git a/main/protocols/websocket_protocol.h b/main/protocols/websocket_protocol.h index 0d4c2bb9..3f32e726 100644 --- a/main/protocols/websocket_protocol.h +++ b/main/protocols/websocket_protocol.h @@ -26,7 +26,7 @@ private: WebSocket* websocket_ = nullptr; void ParseServerHello(const cJSON* root); - void SendText(const std::string& text) override; + bool SendText(const std::string& text) override; }; #endif