#include "asio_service.h"
#include <iostream>
class ChatSession;
{
public:
protected:
void onError(
int error,
const std::string& category,
const std::string& message)
override {
std::cout << "Chat WebSocket server caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
{
public:
protected:
{
std::cout <<
"Chat WebSocketCP session with Id " <<
id() <<
" connected!" << std::endl;
std::string message("Hello from WebSocket chat! Please send a message or '!' to disconnect the client!");
}
{
std::cout <<
"Chat TCP session with Id " <<
id() <<
" disconnected!" << std::endl;
}
{
std::cout << "Incoming: " << message->get_raw_payload() << std::endl;
if (message->get_payload() == "!")
}
void onError(
int error,
const std::string& category,
const std::string& message)
override {
std::cout << "Chat WebSocket session caught an error with code " << error << " and category '" << category << "': " << message << std::endl;
}
};
int main(int argc, char** argv)
{
int port = 4444;
if (argc > 1)
port = std::atoi(argv[1]);
std::cout << "WebSocket server port: " << port << std::endl;
auto service = std::make_shared<AsioService>();
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
std::cout << "Server starting...";
server->Start();
std::cout << "Done!" << std::endl;
std::cout << "Press Enter to stop the server or '!' to restart the server..." << std::endl;
std::string line;
while (getline(std::cin, line))
{
if (line.empty())
break;
if (line == "!")
{
std::cout << "Server restarting...";
server->Restart();
std::cout << "Done!" << std::endl;
continue;
}
line = "(admin) " + line;
server->Multicast(line);
}
std::cout << "Server stopping...";
server->Stop();
std::cout << "Done!" << std::endl;
std::cout << "Asio service stopping...";
service->Stop();
std::cout << "Done!" << std::endl;
return 0;
}