#include "asio_service.h"
#include <iostream>
{
public:
protected:
{
std::cout <<
"Chat WebSocket 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 WebSocket session with Id " <<
id() <<
" disconnected!" << std::endl;
}
{
std::string message((const char*)buffer, size);
std::cout << "Incoming: " << message << std::endl;
std::dynamic_pointer_cast<CppServer::WS::WSServer>(
server())->MulticastText(message);
if (message == "!")
}
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;
}
};
{
public:
protected:
std::shared_ptr<CppServer::Asio::TCPSession>
CreateSession(
const std::shared_ptr<CppServer::Asio::TCPServer>& server)
override
{
return std::make_shared<ChatSession>(std::dynamic_pointer_cast<CppServer::WS::WSServer>(server));
}
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;
}
};
int main(int argc, char** argv)
{
int port = 8080;
if (argc > 1)
port = std::atoi(argv[1]);
std::string www = "../www/ws";
if (argc > 2)
www = argv[2];
std::cout << "WebSocket server port: " << port << std::endl;
std::cout << "WebSocket server static content path: " << www << std::endl;
std::cout << "WebSocket server website: " << "http://localhost:" << port << "/chat/index.html" << std::endl;
std::cout << std::endl;
auto service = std::make_shared<AsioService>();
std::cout << "Asio service starting...";
service->Start();
std::cout << "Done!" << std::endl;
auto server = std::make_shared<ChatServer>(service, port);
server->AddStaticContent(www, "/chat");
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->MulticastText(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;
}
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
std::shared_ptr< TCPServer > & server() noexcept
Get the server.
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
const CppCommon::UUID & id() const noexcept
Get the session Id.
std::shared_ptr< Asio::TCPSession > CreateSession(const std::shared_ptr< Asio::TCPServer > &server) override
WSServer(const WSServer &)=delete
bool SendTextAsync(const void *buffer, size_t size)
WSSession(const std::shared_ptr< WSServer > &server)
virtual void onWSReceived(const void *buffer, size_t size)
Handle WebSocket received notification.
virtual void onWSConnected(const HTTP::HTTPResponse &response)
Handle WebSocket client connected notification.
virtual void onWSDisconnected()
Handle WebSocket client disconnected notification.
WebSocket server definition.