You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
_tcpserver.SetOnConnect ([&_tcpserver] (std::shared_ptr<IConn2> _conn) -> Task<void> {
// Free play here, return then link close, usually `while (true)`// You can register client or unregister, specify the ID based on your profession
_tcpserver.RegisterClient (123, _conn);
_tcpserver.UnregisterClient (123, _conn);
});
// If the handler function has registered internally, the external can send or broadcast messages directly to the corresponding client
std::string _data = "hello";
bool _success = co_await _tcpserver.SendData (123, _data.data (), _data.size ());
size_t _count = co_await _tcpserver.BroadcastData (_data.data (), _data.size ());
Configure SSL context and set new connection handler for SSL server
// Set new connection handler for SSL server
_sslserver.SetOnConnect([&_sslserver](std::shared_ptr<IConn2> _conn) -> Task<void> {
// Handle SSL connection// Send welcome message
std::string welcome_msg = "Welcome to SSL server!\n";
co_await _conn->Send(welcome_msg.data(), welcome_msg.size());
// Register clientstaticint64_t client_id = 1000;
_sslserver.RegisterClient(client_id++, _conn);
// Receive data loopwhile (true) {
try {
char buffer[1024] = {0};
size_t received = co_await _conn->Recv(buffer, sizeof(buffer) - 1);
if (received > 0) {
// Echo received dataco_await _conn->Send(buffer, received);
}
} catch (...) {
break; // Connection closed or other error
}
}
// Unregister client// Note: In real applications, you should maintain client_id mapping according to your business logic
});