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

Class ChatClient

examples/ssl_chat_client.cpp:17–65  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15#include <iostream>
16
17class ChatClient : public CppServer::Asio::SSLClient
18{
19public:
20 using CppServer::Asio::SSLClient::SSLClient;
21
22 void DisconnectAndStop()
23 {
24 _stop = true;
25 DisconnectAsync();
26 while (IsConnected())
27 CppCommon::Thread::Yield();
28 }
29
30protected:
31 void onConnected() override
32 {
33 std::cout << "Chat SSL client connected a new session with Id " << id() << std::endl;
34 }
35
36 void onHandshaked() override
37 {
38 std::cout << "Chat SSL client handshaked a new session with Id " << id() << std::endl;
39 }
40
41 void onDisconnected() override
42 {
43 std::cout << "Chat SSL client disconnected a session with Id " << id() << std::endl;
44
45 // Wait for a while...
46 CppCommon::Thread::Sleep(1000);
47
48 // Try to connect again
49 if (!_stop)
50 ConnectAsync();
51 }
52
53 void onReceived(const void* buffer, size_t size) override
54 {
55 std::cout << "Incoming: " << std::string((const char*)buffer, size) << std::endl;
56 }
57
58 void onError(int error, const std::string& category, const std::string& message) override
59 {
60 std::cout << "Chat SSL client caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
61 }
62
63private:
64 std::atomic<bool> _stop{false};
65};
66
67int main(int argc, char** argv)
68{

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected