I'm trying to understand boost beast by extending the websocket chat-multi example. I understand how it is working now but I do not see how I would access the client connections from some other server side event. For example, the 'admin' running the server enters a message in the terminal. I moved the ws code into a thread while main listens for input but then I'm stuck..
void startServer()
{
auto const address = net::ip::make_address("127.0.0.1");
auto const port = static_cast<unsigned short>(8080);
auto doc_root = ".";
// The io_context is required for all I/O
net::io_context ioc;
log_message("Init io context!");
// Create and launch a listening port
boost::make_shared<listener>(
ioc,
tcp::endpoint{ address, port },
boost::make_shared<shared_state>(doc_root))->run();
// Capture SIGINT and SIGTERM to perform a clean shutdown
net::signal_set signals(ioc, SIGINT, SIGTERM);
signals.async_wait(
[&ioc](boost::system::error_code const&, int)
{
// Stop the io_context. This will cause run()
// to return immediately, eventually destroying the
// io_context and any remaining handlers in it.
ioc.stop();
});
// Run the I/O service on the requested number of threads
std::vector<std::thread> v;
v.reserve(threads - 1);
for (auto i = threads - 1; i > 0; --i)
v.emplace_back(
[&ioc]
{
ioc.run();
});
ioc.run();
// (If we get here, it means we got a SIGINT or SIGTERM)
log_message("SIGINT or SIGTERM, shutting down websocket server");
// Block until all the threads exit
for (auto& t : v)
t.join();
}
int main(int argc, char* argv[])
{
std::thread wsThread = std::thread(startServer);
while(1)
{
std::string msg;
std::cout << "Enter Message: ";
std::getline(std::cin, msg);
// how do I send to all clients?
// shared_state.send(msg);
}
wsThread.join()
}
I did see the other example BeastLounge but that's a bit overwhelming to trace
UPDATE: I think I have a solution - using a wrapper class where I initialize the shared_state ptr, then in a thread start the io ctx and listening port