MCPcopy Create free account
hub / github.com/chronoxor/CppServer / ChatSession

Class ChatSession

examples/ssl_chat_server.cpp:15–57  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

13#include <iostream>
14
15class ChatSession : public CppServer::Asio::SSLSession
16{
17public:
18 using CppServer::Asio::SSLSession::SSLSession;
19
20protected:
21 void onConnected() override
22 {
23 std::cout << "Chat SSL session with Id " << id() << " connected!" << std::endl;
24 }
25
26 void onHandshaked() override
27 {
28 std::cout << "Chat SSL session with Id " << id() << " handshaked!" << std::endl;
29
30 // Send invite message
31 std::string message("Hello from SSL chat! Please send a message or '!' to disconnect the client!");
32 SendAsync(message.data(), message.size());
33 }
34
35 void onDisconnected() override
36 {
37 std::cout << "Chat SSL session with Id " << id() << " disconnected!" << std::endl;
38 }
39
40 void onReceived(const void* buffer, size_t size) override
41 {
42 std::string message((const char*)buffer, size);
43 std::cout << "Incoming: " << message << std::endl;
44
45 // Multicast message to all connected sessions
46 server()->Multicast(message);
47
48 // If the buffer starts with '!' the disconnect the current session
49 if (message == "!")
50 Disconnect();
51 }
52
53 void onError(int error, const std::string& category, const std::string& message) override
54 {
55 std::cout << "Chat SSL session caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
56 }
57};
58
59class ChatServer : public CppServer::Asio::SSLServer
60{

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected