@@ -23,13 +23,15 @@ namespace cqhttp::plugins {
23
23
}
24
24
client->on_close =
25
25
[&](shared_ptr<typename WsClientT::Connection> connection, const int code, const string &reason) {
26
+ connected_ = false ;
26
27
if (reconnect_on_code_1000_ || code != 1000 ) {
27
28
logging::debug (TAG,
28
29
u8" 反向 WebSocket 连接断开,close code: " + to_string (code) + " ,reason:" + reason);
29
30
notify_should_reconnect ();
30
31
}
31
32
};
32
33
client->on_error = [&](shared_ptr<typename WsClientT::Connection>, const SimpleWeb::error_code &e) {
34
+ connected_ = false ;
33
35
logging::debug (TAG, u8" 反向 WebSocket 连接发生错误,error code: " + to_string (e.value ()));
34
36
notify_should_reconnect ();
35
37
};
@@ -68,13 +70,15 @@ namespace cqhttp::plugins {
68
70
logging::debug (TAG, u8" 反向 WebSocket 建立连接失败" );
69
71
notify_should_reconnect ();
70
72
}
73
+ connected_ = false ;
71
74
started_ = false ;
72
75
});
73
76
logging::info_success (TAG, u8" 开启反向 WebSocket 客户端(" + name () + u8" )成功,开始连接 " + url_);
74
77
}
75
78
}
76
79
77
80
void WebSocketReverse::ClientBase::disconnect () {
81
+ connected_ = false ;
78
82
if (started_) {
79
83
if (client_is_wss_.value () == false ) {
80
84
client_.ws ->stop ();
@@ -171,11 +175,13 @@ namespace cqhttp::plugins {
171
175
172
176
if (client_is_wss_.has_value ()) {
173
177
if (client_is_wss_.value () == false ) {
178
+ client_.ws ->on_open = [&](auto ) { connected_ = true ; };
174
179
client_.ws ->on_message = [&connection_mutex = client_.ws ->connection_mutex ](auto connection,
175
180
auto message) {
176
181
api_on_message<WsClient>(connection_mutex, connection, message);
177
182
};
178
183
} else {
184
+ client_.wss ->on_open = [&](auto ) { connected_ = true ; };
179
185
client_.wss ->on_message = [&connection_mutex = client_.wss ->connection_mutex ](auto connection,
180
186
auto message) {
181
187
api_on_message<WssClient>(connection_mutex, connection, message);
@@ -189,51 +195,56 @@ namespace cqhttp::plugins {
189
195
190
196
if (client_is_wss_.has_value ()) {
191
197
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 ;
193
200
emit_lifecycle_meta_event (MetaEvent::SubType::LIFECYCLE_CONNECT);
194
201
};
195
202
} 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 ;
197
205
emit_lifecycle_meta_event (MetaEvent::SubType::LIFECYCLE_CONNECT);
198
206
};
199
207
}
200
208
}
201
209
}
202
210
203
211
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
+ }
206
216
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 ;
236
228
}
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" 失败" );
237
248
}
238
249
}
239
250
0 commit comments