Skip to content

Commit

Permalink
服务端主动发送 Ping 探测连接性
Browse files Browse the repository at this point in the history
  • Loading branch information
lanthora committed Dec 2, 2023
1 parent aa8532f commit 12abca1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/core/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ void Server::handleAuthMessage(WebSocketMessage &message) {
}

spdlog::info("client connected: {}", address.getIpStr());
this->ws.keepalive(message.conn);
this->ipWsMap[address.getIp()] = message.conn;
this->wsIpMap[message.conn] = address.getIp();
}
Expand Down
15 changes: 15 additions & 0 deletions src/websocket/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,15 @@ class WebSockeServerImpl {
return 0;
}

int keepalive(WebSocketConn conn) {
std::weak_ptr<ix::WebSocket> weakConn = std::any_cast<std::weak_ptr<ix::WebSocket>>(conn.conn);
auto ws = weakConn.lock();
if (ws) {
ws->setPingInterval(30);
}
return 0;
}

int close(WebSocketConn conn) {
std::weak_ptr<ix::WebSocket> weakConn = std::any_cast<std::weak_ptr<ix::WebSocket>>(conn.conn);
auto ws = weakConn.lock();
Expand Down Expand Up @@ -169,4 +178,10 @@ int WebSocketServer::close(WebSocketConn conn) {
return server->close(conn);
}

int WebSocketServer::keepalive(WebSocketConn conn) {
std::shared_ptr<WebSockeServerImpl> server;
server = std::any_cast<std::shared_ptr<WebSockeServerImpl>>(this->impl);
return server->keepalive(conn);
}

} // namespace Candy
3 changes: 3 additions & 0 deletions src/websocket/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ class WebSocketServer {
// 关闭单个客户端连接
int close(WebSocketConn conn);

// 服务端主动探测客户端连接状态
int keepalive(WebSocketConn conn);

private:
std::any impl;
};
Expand Down

0 comments on commit 12abca1

Please sign in to comment.