mirror of
https://github.com/kunkundi/crossdesk-server.git
synced 2026-03-19 13:52:40 +08:00
[feat] bind websocket id to user id when receive login request
This commit is contained in:
@@ -47,9 +47,9 @@ bool SignalServer::on_open(websocketpp::connection_hdl hdl) {
|
||||
|
||||
LOG_INFO("New websocket connection [{}] established", ws_connection_id_);
|
||||
|
||||
json message = {{"type", "ws_connection_id"},
|
||||
{"ws_connection_id", ws_connection_id_}};
|
||||
server_.send(hdl, message.dump(), websocketpp::frame::opcode::text);
|
||||
// json message = {{"type", "ws_connection_id"},
|
||||
// {"ws_connection_id", ws_connection_id_}};
|
||||
// server_.send(hdl, message.dump(), websocketpp::frame::opcode::text);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -113,6 +113,8 @@ void SignalServer::run(uint16_t port) {
|
||||
void SignalServer::send_msg(websocketpp::connection_hdl hdl, json message) {
|
||||
if (!hdl.expired()) {
|
||||
server_.send(hdl, message.dump(), websocketpp::frame::opcode::text);
|
||||
} else {
|
||||
LOG_ERROR("Destination hdl invalid");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -125,13 +127,34 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
std::string type = j["type"].get<std::string>();
|
||||
|
||||
switch (HASH_STRING_PIECE(type.c_str())) {
|
||||
case "login"_H: {
|
||||
std::string host_id = j["user_id"].get<std::string>();
|
||||
if (host_id.empty()) {
|
||||
host_id = client_id_generator_.GeneratorNewId();
|
||||
LOG_INFO("New client, assign id [{}] to it", host_id);
|
||||
}
|
||||
|
||||
LOG_INFO("Receive host id [{}] login request with id [{}]", host_id);
|
||||
bool success = transmission_manager_.BindUserToWsHandle(host_id, hdl);
|
||||
if (success) {
|
||||
json message = {
|
||||
{"type", "login"}, {"user_id", host_id}, {"status", "success"}};
|
||||
send_msg(hdl, message);
|
||||
} else {
|
||||
json message = {
|
||||
{"type", "login"}, {"user_id", host_id}, {"status", "fail"}};
|
||||
send_msg(hdl, message);
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
case "create_transmission"_H: {
|
||||
std::string transmission_id = j["transmission_id"].get<std::string>();
|
||||
std::string password = j["password"].get<std::string>();
|
||||
std::string host_id = j["user_id"].get<std::string>();
|
||||
LOG_INFO(
|
||||
"Receive host id [{}|{}] create transmission request with id [{}]",
|
||||
host_id, hdl.lock().get(), transmission_id);
|
||||
|
||||
LOG_INFO("Receive host id [{}] create transmission request with id [{}]",
|
||||
host_id, transmission_id);
|
||||
if (!transmission_manager_.IsTransmissionExist(transmission_id)) {
|
||||
if (transmission_id.empty()) {
|
||||
transmission_id = GenerateTransmissionId();
|
||||
@@ -145,7 +168,6 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
}
|
||||
|
||||
transmission_manager_.BindHostToTransmission(host_id, transmission_id);
|
||||
transmission_manager_.BindUserToWsHandle(host_id, hdl);
|
||||
transmission_manager_.BindPasswordToTransmission(password,
|
||||
transmission_id);
|
||||
|
||||
@@ -251,7 +273,6 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
std::string user_id = j["user_id"].get<std::string>();
|
||||
|
||||
transmission_manager_.BindGuestToTransmission(user_id, transmission_id);
|
||||
transmission_manager_.BindUserToWsHandle(user_id, hdl);
|
||||
|
||||
websocketpp::connection_hdl destination_hdl =
|
||||
transmission_manager_.GetWsHandle(remote_user_id);
|
||||
@@ -304,7 +325,7 @@ void SignalServer::on_message(websocketpp::connection_hdl hdl,
|
||||
websocketpp::connection_hdl destination_hdl =
|
||||
transmission_manager_.GetWsHandle(remote_user_id);
|
||||
|
||||
LOG_INFO("send candidate [{}]", candidate.c_str());
|
||||
// LOG_INFO("send candidate [{}]", candidate.c_str());
|
||||
json message = {{"type", "new_candidate"},
|
||||
{"sdp", candidate},
|
||||
{"remote_user_id", user_id},
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <websocketpp/config/asio_no_tls.hpp>
|
||||
#include <websocketpp/server.hpp>
|
||||
|
||||
#include "client_id_generator.h"
|
||||
#include "transmission_manager.h"
|
||||
|
||||
using nlohmann::json;
|
||||
@@ -43,7 +44,9 @@ class SignalServer {
|
||||
ws_connections_;
|
||||
unsigned int ws_connection_id_ = 0;
|
||||
|
||||
private:
|
||||
TransmissionManager transmission_manager_;
|
||||
ClientIdGenerator client_id_generator_;
|
||||
};
|
||||
|
||||
#endif
|
||||
@@ -120,7 +120,7 @@ bool TransmissionManager::BindHostToTransmission(
|
||||
if (transmission_host_id_list_.find(transmission_id) ==
|
||||
transmission_host_id_list_.end()) {
|
||||
transmission_host_id_list_[transmission_id] = host_id;
|
||||
LOG_INFO("Bind host id [{}] to transmission [{}]", host_id,
|
||||
LOG_INFO("Bind host id [{}] to transmission [{}]", host_id,
|
||||
transmission_id);
|
||||
return true;
|
||||
} else {
|
||||
@@ -297,11 +297,14 @@ int TransmissionManager::UpdateWsHandleLastActiveTime(
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t now_time =
|
||||
std::chrono::system_clock::now().time_since_epoch().count();
|
||||
uint32_t now_time = std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count();
|
||||
ws_hdl_last_active_time_list_.push_front(std::make_pair(hdl, now_time));
|
||||
ws_hdl_iter_list_[hdl] = ws_hdl_last_active_time_list_.begin();
|
||||
|
||||
// LOG_INFO("Update [{}] with time [{}]", hdl.lock().get(), now_time);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -317,7 +320,9 @@ void TransmissionManager::AliveChecker() {
|
||||
}
|
||||
|
||||
uint32_t now_time =
|
||||
std::chrono::system_clock::now().time_since_epoch().count();
|
||||
std::chrono::duration_cast<std::chrono::seconds>(
|
||||
std::chrono::system_clock::now().time_since_epoch())
|
||||
.count();
|
||||
|
||||
uint32_t duration =
|
||||
now_time - ws_hdl_last_active_time_list_.back().second;
|
||||
@@ -325,7 +330,11 @@ void TransmissionManager::AliveChecker() {
|
||||
bool is_dead = duration > 100000000 ? true : false;
|
||||
|
||||
if (is_dead) {
|
||||
LOG_INFO("Websocket handle [{}] is dead", hdl.lock().get());
|
||||
LOG_INFO(
|
||||
"Websocket handle [{}] is dead, now time [{}], last active time "
|
||||
"[{}], duration [{}]",
|
||||
hdl.lock().get(), now_time,
|
||||
ws_hdl_last_active_time_list_.back().second, duration);
|
||||
if (ws_hdl_iter_list_.find(hdl) != ws_hdl_iter_list_.end()) {
|
||||
auto it = ws_hdl_iter_list_[hdl];
|
||||
|
||||
|
||||
Reference in New Issue
Block a user