mirror of
https://github.com/kunkundi/crossdesk.git
synced 2025-10-26 20:25:34 +08:00
Notify other users when a user websocket closed
This commit is contained in:
@@ -55,8 +55,20 @@ bool SignalServer::on_open(websocketpp::connection_hdl hdl) {
|
||||
|
||||
bool SignalServer::on_close(websocketpp::connection_hdl hdl) {
|
||||
LOG_INFO("Websocket onnection [{}] closed", ws_connection_id_);
|
||||
bool ret = transmission_manager_.ReleaseUserIdFromTransmission(hdl);
|
||||
std::string transmission_id =
|
||||
transmission_manager_.ReleaseUserIdFromTransmission(hdl);
|
||||
|
||||
std::vector<std::string> user_id_list =
|
||||
transmission_manager_.GetAllUserIdOfTransmission(transmission_id);
|
||||
json message = {{"type", "user_leave_transmission"},
|
||||
{"transmission_id", transmission_id},
|
||||
{"user_id", transmission_manager_.GetUserId(hdl)}};
|
||||
|
||||
for (const auto& user_id : user_id_list) {
|
||||
send_msg(transmission_manager_.GetWsHandle(user_id), message);
|
||||
}
|
||||
ws_connections_.erase(hdl);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -52,24 +52,7 @@ bool TransmissionManager::BindUserIdToWsHandle(
|
||||
return true;
|
||||
}
|
||||
|
||||
bool TransmissionManager::ReleaseWsHandleFromTransmission(
|
||||
websocketpp::connection_hdl hdl) {
|
||||
for (auto trans_it = transmission_user_ws_hdl_list_.begin();
|
||||
trans_it != transmission_user_ws_hdl_list_.end(); ++trans_it) {
|
||||
auto hdl_list = trans_it->second;
|
||||
for (auto it = hdl_list.begin(); it != hdl_list.end(); ++it) {
|
||||
if (it->lock().get() == hdl.lock().get()) {
|
||||
hdl_list.erase(it);
|
||||
LOG_INFO("Remove ws handle [{}] from transmission [{}]",
|
||||
hdl.lock().get(), trans_it->first);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool TransmissionManager::ReleaseUserIdFromTransmission(
|
||||
std::string TransmissionManager::ReleaseUserIdFromTransmission(
|
||||
websocketpp::connection_hdl hdl) {
|
||||
for (auto it = user_id_ws_hdl_list_.begin(); it != user_id_ws_hdl_list_.end();
|
||||
++it) {
|
||||
@@ -84,12 +67,12 @@ bool TransmissionManager::ReleaseUserIdFromTransmission(
|
||||
LOG_INFO("Remove user id [{}] from transmission [{}]", it->first,
|
||||
trans_it->first);
|
||||
user_id_ws_hdl_list_.erase(it);
|
||||
return true;
|
||||
return trans_it->first;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return "";
|
||||
}
|
||||
|
||||
websocketpp::connection_hdl TransmissionManager::GetWsHandle(
|
||||
@@ -101,3 +84,11 @@ websocketpp::connection_hdl TransmissionManager::GetWsHandle(
|
||||
return hdl;
|
||||
}
|
||||
}
|
||||
|
||||
std::string TransmissionManager::GetUserId(websocketpp::connection_hdl hdl) {
|
||||
for (auto it = user_id_ws_hdl_list_.begin(); it != user_id_ws_hdl_list_.end();
|
||||
++it) {
|
||||
if (it->second.lock().get() == hdl.lock().get()) return it->first;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@@ -20,28 +20,12 @@ class TransmissionManager {
|
||||
bool BindUserIdToWsHandle(const std::string& user_id,
|
||||
websocketpp::connection_hdl hdl);
|
||||
|
||||
bool ReleaseWsHandleFromTransmission(websocketpp::connection_hdl hdl);
|
||||
bool ReleaseUserIdFromTransmission(websocketpp::connection_hdl hdl);
|
||||
std::string ReleaseUserIdFromTransmission(websocketpp::connection_hdl hdl);
|
||||
|
||||
websocketpp::connection_hdl GetWsHandle(const std::string& user_id);
|
||||
std::string GetUserId(websocketpp::connection_hdl hdl);
|
||||
|
||||
private:
|
||||
std::map<std::string, websocketpp::connection_hdl> transmission_host_list_;
|
||||
std::map<std::string, std::vector<websocketpp::connection_hdl>>
|
||||
transmission_guest_list_;
|
||||
|
||||
std::map<std::string, websocketpp::connection_hdl>
|
||||
transmission_host_username_list_;
|
||||
std::map<std::string, websocketpp::connection_hdl>
|
||||
transmission_guest_username_list_;
|
||||
|
||||
private:
|
||||
std::map<std::string, std::vector<websocketpp::connection_hdl>>
|
||||
transmission_user_ws_hdl_list_;
|
||||
std::map<std::string, websocketpp::connection_hdl> username_ws_hdl_list_;
|
||||
|
||||
std::map<std::string, std::vector<std::string>> user_name_user_id_list_;
|
||||
|
||||
std::map<std::string, std::vector<std::string>> transmission_user_id_list_;
|
||||
std::map<std::string, websocketpp::connection_hdl> user_id_ws_hdl_list_;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user