| 13 | #include <iostream> |
| 14 | |
| 15 | class ChatSession : public CppServer::WS::WSSession |
| 16 | { |
| 17 | public: |
| 18 | using CppServer::WS::WSSession::WSSession; |
| 19 | |
| 20 | protected: |
| 21 | void onWSConnected(const CppServer::HTTP::HTTPRequest& request) override |
| 22 | { |
| 23 | std::cout << "Chat WebSocket session with Id " << id() << " connected!" << std::endl; |
| 24 | |
| 25 | // Send invite message |
| 26 | std::string message("Hello from WebSocket chat! Please send a message or '!' to disconnect the client!"); |
| 27 | SendTextAsync(message); |
| 28 | } |
| 29 | |
| 30 | void onWSDisconnected() override |
| 31 | { |
| 32 | std::cout << "Chat WebSocket session with Id " << id() << " disconnected!" << std::endl; |
| 33 | } |
| 34 | |
| 35 | void onWSReceived(const void* buffer, size_t size) override |
| 36 | { |
| 37 | std::string message((const char*)buffer, size); |
| 38 | std::cout << "Incoming: " << message << std::endl; |
| 39 | |
| 40 | // Multicast message to all connected sessions |
| 41 | std::dynamic_pointer_cast<CppServer::WS::WSServer>(server())->MulticastText(message); |
| 42 | |
| 43 | // If the buffer starts with '!' the disconnect the current session |
| 44 | if (message == "!") |
| 45 | Close(); |
| 46 | } |
| 47 | |
| 48 | void onError(int error, const std::string& category, const std::string& message) override |
| 49 | { |
| 50 | std::cout << "Chat WebSocket session caught an error with code " << error << " and category '" << category << "': " << message << std::endl; |
| 51 | } |
| 52 | }; |
| 53 | |
| 54 | class ChatServer : public CppServer::WS::WSServer |
| 55 | { |
nothing calls this directly
no outgoing calls
no test coverage detected