@@ -23,13 +23,15 @@ namespace cqhttp::plugins {
2323 }
2424 client->on_close =
2525 [&](shared_ptr<typename WsClientT::Connection> connection, const int code, const string &reason) {
26+ connected_ = false ;
2627 if (reconnect_on_code_1000_ || code != 1000 ) {
2728 logging::debug (TAG,
2829 u8" 反向 WebSocket 连接断开,close code: " + to_string (code) + " ,reason:" + reason);
2930 notify_should_reconnect ();
3031 }
3132 };
3233 client->on_error = [&](shared_ptr<typename WsClientT::Connection>, const SimpleWeb::error_code &e) {
34+ connected_ = false ;
3335 logging::debug (TAG, u8" 反向 WebSocket 连接发生错误,error code: " + to_string (e.value ()));
3436 notify_should_reconnect ();
3537 };
@@ -68,13 +70,15 @@ namespace cqhttp::plugins {
6870 logging::debug (TAG, u8" 反向 WebSocket 建立连接失败" );
6971 notify_should_reconnect ();
7072 }
73+ connected_ = false ;
7174 started_ = false ;
7275 });
7376 logging::info_success (TAG, u8" 开启反向 WebSocket 客户端(" + name () + u8" )成功,开始连接 " + url_);
7477 }
7578 }
7679
7780 void WebSocketReverse::ClientBase::disconnect () {
81+ connected_ = false ;
7882 if (started_) {
7983 if (client_is_wss_.value () == false ) {
8084 client_.ws ->stop ();
@@ -171,11 +175,13 @@ namespace cqhttp::plugins {
171175
172176 if (client_is_wss_.has_value ()) {
173177 if (client_is_wss_.value () == false ) {
178+ client_.ws ->on_open = [&](auto ) { connected_ = true ; };
174179 client_.ws ->on_message = [&connection_mutex = client_.ws ->connection_mutex ](auto connection,
175180 auto message) {
176181 api_on_message<WsClient>(connection_mutex, connection, message);
177182 };
178183 } else {
184+ client_.wss ->on_open = [&](auto ) { connected_ = true ; };
179185 client_.wss ->on_message = [&connection_mutex = client_.wss ->connection_mutex ](auto connection,
180186 auto message) {
181187 api_on_message<WssClient>(connection_mutex, connection, message);
@@ -189,51 +195,56 @@ namespace cqhttp::plugins {
189195
190196 if (client_is_wss_.has_value ()) {
191197 if (client_is_wss_.value () == false ) {
192- client_.ws ->on_open = [](const shared_ptr<WsClient::Connection> connection) {
198+ client_.ws ->on_open = [&](const shared_ptr<WsClient::Connection> connection) {
199+ connected_ = true ;
193200 emit_lifecycle_meta_event (MetaEvent::SubType::LIFECYCLE_CONNECT);
194201 };
195202 } else {
196- client_.ws ->on_open = [](const shared_ptr<WsClient::Connection> connection) {
203+ client_.ws ->on_open = [&](const shared_ptr<WsClient::Connection> connection) {
204+ connected_ = true ;
197205 emit_lifecycle_meta_event (MetaEvent::SubType::LIFECYCLE_CONNECT);
198206 };
199207 }
200208 }
201209 }
202210
203211 void WebSocketReverse::EventClient::push_event (const json &payload) {
204- if (started_) {
205- logging::debug (TAG, u8" 开始通过反向 WebSocket 客户端上报事件" );
212+ if (!connected_) {
213+ logging::info (TAG, u8" 反向 WebSocket 连接尚未建立,无法上报" );
214+ return ;
215+ }
206216
207- const auto send_cb = [=](const SimpleWeb::error_code &ec) {
208- if (!ec) {
209- logging::info_success (TAG, u8" 通过反向 WebSocket 客户端上报数据到 " + url_ + u8" 成功" );
210- } else {
211- logging::warning (TAG,
212- u8" 通过反向 WebSocket 客户端上报数据到 " + url_ + u8" 失败,错误码:"
213- + std::to_string (ec.value ()) + u8" ,将尝试重连" );
214- std::unique_lock<std::mutex> lock (mutex_);
215- should_reconnect_ = true ;
216- }
217- };
218- try {
219- if (client_is_wss_.value () == false ) {
220- const auto out_message = make_shared<WsClient::OutMessage>();
221- *out_message << payload.dump ();
222- // the WsClient class is modified by us ("connection" property made public),
223- // so we must maintain the lock manually
224- unique_lock<mutex> lock (client_.ws ->connection_mutex );
225- client_.ws ->connection ->send (out_message, send_cb); // TODO: send 失败应当重新连接
226- lock.unlock ();
227- } else {
228- const auto out_message = make_shared<WssClient::OutMessage>();
229- *out_message << payload.dump ();
230- unique_lock<mutex> lock (client_.wss ->connection_mutex );
231- client_.wss ->connection ->send (out_message, send_cb);
232- lock.unlock ();
233- }
234- } catch (...) {
235- logging::warning (TAG, u8" 通过反向 WebSocket 客户端上报数据到 " + url_ + u8" 失败" );
217+ logging::debug (TAG, u8" 开始通过反向 WebSocket 客户端上报事件" );
218+
219+ const auto send_cb = [=](const SimpleWeb::error_code &ec) {
220+ if (!ec) {
221+ logging::info_success (TAG, u8" 通过反向 WebSocket 客户端上报数据到 " + url_ + u8" 成功" );
222+ } else {
223+ logging::warning (TAG,
224+ u8" 通过反向 WebSocket 客户端上报数据到 " + url_ + u8" 失败,错误码:"
225+ + std::to_string (ec.value ()) + u8" ,将尝试重连" );
226+ std::unique_lock<std::mutex> lock (mutex_);
227+ should_reconnect_ = true ;
236228 }
229+ };
230+ try {
231+ if (client_is_wss_.value () == false ) {
232+ const auto out_message = make_shared<WsClient::OutMessage>();
233+ *out_message << payload.dump ();
234+ // the WsClient class is modified by us ("connection" property made public),
235+ // so we must maintain the lock manually
236+ unique_lock<mutex> lock (client_.ws ->connection_mutex );
237+ client_.ws ->connection ->send (out_message, send_cb); // TODO: send 失败应当重新连接
238+ lock.unlock ();
239+ } else {
240+ const auto out_message = make_shared<WssClient::OutMessage>();
241+ *out_message << payload.dump ();
242+ unique_lock<mutex> lock (client_.wss ->connection_mutex );
243+ client_.wss ->connection ->send (out_message, send_cb);
244+ lock.unlock ();
245+ }
246+ } catch (...) {
247+ logging::warning (TAG, u8" 通过反向 WebSocket 客户端上报数据到 " + url_ + u8" 失败" );
237248 }
238249 }
239250
0 commit comments