Add websocket class

This commit is contained in:
2024-08-19 21:29:22 +02:00
parent 352f25abe4
commit d67108542e
4 changed files with 121 additions and 2 deletions
+2 -1
View File
@@ -3,6 +3,7 @@ target_sources(simulators PRIVATE
${CMAKE_CURRENT_LIST_DIR}/main.cpp ${CMAKE_CURRENT_LIST_DIR}/main.cpp
${CMAKE_CURRENT_LIST_DIR}/simulator_base.cpp ${CMAKE_CURRENT_LIST_DIR}/simulator_base.cpp
${CMAKE_CURRENT_LIST_DIR}/simulators.cpp ${CMAKE_CURRENT_LIST_DIR}/simulators.cpp
${CMAKE_CURRENT_LIST_DIR}/websocket.cpp
) )
target_compile_options(simulators PRIVATE $<$<CONFIG:Debug>: target_compile_options(simulators PRIVATE $<$<CONFIG:Debug>:
@@ -13,7 +14,7 @@ target_include_directories(simulators
PUBLIC PUBLIC
${CMAKE_CURRENT_LIST_DIR} ${CMAKE_CURRENT_LIST_DIR}
PRIVATE PRIVATE
${PROJECT_DIR}/external/** ${PROJECT_DIR}/external/websocketpp
) )
target_link_libraries(simulators PRIVATE target_link_libraries(simulators PRIVATE
+35 -1
View File
@@ -1,5 +1,39 @@
#include <iostream>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include "simulators.hpp"
#include "websocket.hpp"
Simulators simulators;
Websocket* g_web_socket;
void int_handler(int s)
{
UNUSED(s);
std::cout << "\nCaught Ctrl + C\nShutting down." << std::endl;
g_web_socket->stop();
exit(1);
}
int main(void) int main(void)
{ {
return 0; struct sigaction sigIntHandler;
sigIntHandler.sa_handler = int_handler;
sigemptyset(&sigIntHandler.sa_mask);
sigIntHandler.sa_flags = 0;
sigaction(SIGINT, &sigIntHandler, NULL);
Websocket web_socket(9002, [](std::string message) {
return message;
});
g_web_socket = &web_socket;
web_socket.run();
} }
+60
View File
@@ -0,0 +1,60 @@
#include "websocket.hpp"
Websocket::Websocket(uint16_t port, std::function<std::string(std::string)> on_message)
: p_on_message(on_message)
{
try {
// Set logging settings
server.set_access_channels(websocketpp::log::alevel::all);
server.clear_access_channels(websocketpp::log::alevel::frame_payload);
// Initialize Asio
server.init_asio();
// Register our message handler
server.set_message_handler(bind(&Websocket::on_message, this, &server, ::_1, ::_2));
// Listen on port 9002
server.listen(port);
} catch (websocketpp::exception const & e) {
std::cout << e.what() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}
void Websocket::run()
{
try
{
// Start the server accept loop
server.start_accept();
// Start the ASIO io_service run loop
server.run();
} catch (websocketpp::exception const & e) {
std::cout << e.what() << std::endl;
} catch (...) {
std::cout << "other exception" << std::endl;
}
}
void Websocket::stop()
{
server.stop_listening();
server.stop();
}
void Websocket::on_message(Server* s, websocketpp::connection_hdl hdl, Server::message_ptr msg) {
std::cout << "on_message called with hdl: " << hdl.lock().get()
<< " and message: " << msg->get_payload()
<< std::endl;
std::string response = p_on_message(msg->get_payload());
try {
s->send(hdl, response, msg->get_opcode());
} catch (websocketpp::exception const & e) {
std::cout << "Response failed because: "
<< "(" << e.what() << ")" << std::endl;
}
}
+24
View File
@@ -0,0 +1,24 @@
#pragma once
#include <functional>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
typedef websocketpp::server<websocketpp::config::asio> Server;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
class Websocket
{
public:
Websocket(uint16_t port, std::function<std::string(std::string)> on_message);
void run();
void stop();
private:
void on_message(Server* s, websocketpp::connection_hdl hdl, Server::message_ptr msg);
Server server;
std::function<std::string(std::string)> p_on_message;
};