Skip to content
This repository was archived by the owner on Aug 2, 2020. It is now read-only.

Commit 46e29f8

Browse files
committed
Reverse websocket detail done
1 parent dc358ce commit 46e29f8

File tree

4 files changed

+26
-25
lines changed

4 files changed

+26
-25
lines changed

src/api/handlers.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,8 @@ HANDLER(get_status) {
372372
{"app_enabled", app.is_enabled()},
373373
{"server_initialized", ApiServer::instance().is_initialized()},
374374
{"http_server_started", ApiServer::instance().http_server_is_started()},
375-
{"ws_server_started", ApiServer::instance().ws_server_is_started()}
375+
{"ws_server_started", ApiServer::instance().ws_server_is_started()},
376+
{"ws_reverse_api_client_started", ApiServer::instance().ws_reverse_api_client_is_started()}
376377
};
377378
}
378379

src/api/server_class.cpp

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ void ApiServer::start() {
395395
}
396396

397397
if (config.use_ws_reverse /* use ws reverse */
398-
&& ws_reverse_api_client_is_wss_.has_value() /* client successfully initialized */) {
398+
&& ws_reverse_api_client_is_wss_.has_value() /* successfully initialized */) {
399399
ws_reverse_api_client_started_ = true;
400400
ws_reverse_api_thread_ = thread([&]() {
401401
if (ws_reverse_api_client_is_wss_.value() == false) {
@@ -466,28 +466,31 @@ static bool push_ws_reverse_event(const string &server_port_path, const json &pa
466466
return succeeded;
467467
}
468468

469-
size_t ApiServer::push_event(const json &payload) const {
470-
if (!ws_server_started_) {
471-
return 0;
472-
}
473-
474-
size_t count = 0;
475-
for (const auto &connection : ws_server_->get_connections()) {
476-
if (boost::algorithm::starts_with(connection->path, "/event")) {
477-
const auto send_stream = make_shared<WsServer::SendStream>();
478-
*send_stream << payload.dump();
479-
connection->send(send_stream);
480-
count++;
469+
void ApiServer::push_event(const json &payload) const {
470+
if (ws_server_started_) {
471+
Log::d(TAG, u8"开始通过 WebSocket 服务端推送事件");
472+
size_t count = 0;
473+
for (const auto &connection : ws_server_->get_connections()) {
474+
if (boost::algorithm::starts_with(connection->path, "/event")) {
475+
const auto send_stream = make_shared<WsServer::SendStream>();
476+
*send_stream << payload.dump();
477+
connection->send(send_stream);
478+
count++;
479+
}
481480
}
481+
Log::d(TAG, u8"已成功向 " + to_string(count) + u8" 个客户端推送事件");
482482
}
483483

484-
if (ws_reverse_event_client_is_wss_.has_value()) {
484+
if (config.use_ws_reverse /* use ws reverse */
485+
&& ws_reverse_event_client_is_wss_.has_value() /* successfully initialized */) {
486+
Log::d(TAG, u8"开始通过 WebSocket 反向客户端上报事件");
487+
bool succeeded;
485488
if (ws_reverse_event_client_is_wss_.value() == false) {
486-
push_ws_reverse_event<WsClient>(ws_reverse_event_server_port_path_, payload);
489+
succeeded = push_ws_reverse_event<WsClient>(ws_reverse_event_server_port_path_, payload);
487490
} else {
488-
push_ws_reverse_event<WssClient>(ws_reverse_event_server_port_path_, payload);
491+
succeeded = push_ws_reverse_event<WssClient>(ws_reverse_event_server_port_path_, payload);
489492
}
490-
}
491493

492-
return count;
494+
Log::d(TAG, u8"通过 WebSocket 反向客户端上报数据到 " + config.ws_reverse_event_url + (succeeded ? u8" 成功" : u8" 失败"));
495+
}
493496
}

src/api/server_class.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,10 @@ class ApiServer {
3939
bool is_initialized() const { return initialized_; }
4040
bool http_server_is_started() const { return http_server_started_; }
4141
bool ws_server_is_started() const { return ws_server_started_; }
42+
bool ws_reverse_api_client_is_started() const { return ws_reverse_api_client_started_; }
4243

4344
// websocket only
44-
size_t push_event(const json &payload) const;
45+
void push_event(const json &payload) const;
4546

4647
private:
4748
ApiServer() {}

src/event/events.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,7 @@ static int32_t post_event(const json &payload, const function<void(const Params
9797
}
9898
}
9999

100-
if (ApiServer::instance().ws_server_is_started()) {
101-
Log::d(TAG, u8"开始通过 WebSocket 推送事件");
102-
const auto client_count = ApiServer::instance().push_event(payload);
103-
Log::d(TAG, u8"已成功向 " + to_string(client_count) + u8" 个客户端推送事件");
104-
}
100+
ApiServer::instance().push_event(payload);
105101

106102
return should_block ? CQEVENT_BLOCK : CQEVENT_IGNORE;
107103
}

0 commit comments

Comments
 (0)