|
@@ -1,4 +1,5 @@
|
|
|
#include <boost/asio.hpp>
|
|
|
+#include <boost/asio/signal_set.hpp>
|
|
|
#include <iostream>
|
|
|
#include "WebsocketServer.h"
|
|
|
#include <boost/asio/ip/tcp.hpp>
|
|
@@ -7,17 +8,32 @@ using tcp = boost::asio::ip::tcp;
|
|
|
|
|
|
int main() {
|
|
|
try {
|
|
|
+
|
|
|
+ boost::asio::io_context io_context;
|
|
|
+
|
|
|
+ boost::asio::signal_set signals(io_context, SIGINT, SIGTERM);
|
|
|
+
|
|
|
|
|
|
tcp::endpoint endpoint(tcp::v4(), 9000);
|
|
|
WebsocketServer server( endpoint);
|
|
|
server.start();
|
|
|
|
|
|
-
|
|
|
- std::cout << "Press Enter to stop the server..." << std::endl;
|
|
|
- std::cin.get();
|
|
|
+
|
|
|
+ signals.async_wait(
|
|
|
+ [&](const boost::system::error_code& error, int signal_number) {
|
|
|
+ if (!error) {
|
|
|
+ std::cout << "\nSignal " << signal_number << " received. Stopping server..." << std::endl;
|
|
|
+ server.stop();
|
|
|
+ io_context.stop();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ );
|
|
|
|
|
|
- server.stop();
|
|
|
- } catch (const std::exception& ex) {
|
|
|
+ std::cout << "Server is running. Press Ctrl+C to exit..." << std::endl;
|
|
|
+
|
|
|
+ io_context.run();
|
|
|
+ }
|
|
|
+ catch (const std::exception& ex) {
|
|
|
std::cerr << "[Main] Exception: " << ex.what() << std::endl;
|
|
|
}
|
|
|
return 0;
|