mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +01:00
websocket
split broadcast out of message function
This commit is contained in:
@@ -38,9 +38,11 @@ int main(void)
|
|||||||
g_web_socket = &web_socket;
|
g_web_socket = &web_socket;
|
||||||
|
|
||||||
std::thread message_processor_thread(std::bind(&Websocket::process_messages, &web_socket));
|
std::thread message_processor_thread(std::bind(&Websocket::process_messages, &web_socket));
|
||||||
|
std::thread broadcast_processor_thred(std::bind(&Websocket::process_broadcast, &web_socket));
|
||||||
|
|
||||||
// Run the asio loop with the main thread
|
// Run the asio loop with the main thread
|
||||||
web_socket.run();
|
web_socket.run();
|
||||||
|
|
||||||
message_processor_thread.join();
|
message_processor_thread.join();
|
||||||
|
broadcast_processor_thred.join();
|
||||||
}
|
}
|
||||||
+40
-24
@@ -1,7 +1,8 @@
|
|||||||
#include "websocket.hpp"
|
#include "websocket.hpp"
|
||||||
|
|
||||||
|
Websocket::Websocket(uint16_t port,
|
||||||
Websocket::Websocket(uint16_t port, std::function<std::string(std::string)> on_message, std::function<std::string()> on_update)
|
std::function<std::string(std::string)> on_message,
|
||||||
|
std::function<std::string()> on_update)
|
||||||
: m_on_message(on_message), m_on_update(on_update), m_port(port)
|
: m_on_message(on_message), m_on_update(on_update), m_port(port)
|
||||||
{
|
{
|
||||||
// Set logging settings
|
// Set logging settings
|
||||||
@@ -27,7 +28,7 @@ void Websocket::run()
|
|||||||
{
|
{
|
||||||
// Start the ASIO io_service run loop
|
// Start the ASIO io_service run loop
|
||||||
m_server.run();
|
m_server.run();
|
||||||
} catch (websocketpp::exception const & e) {
|
} catch (websocketpp::exception const& e) {
|
||||||
std::cout << e.what() << std::endl;
|
std::cout << e.what() << std::endl;
|
||||||
} catch (...) {
|
} catch (...) {
|
||||||
std::cout << "other exception" << std::endl;
|
std::cout << "other exception" << std::endl;
|
||||||
@@ -44,8 +45,8 @@ void Websocket::on_open(websocketpp::connection_hdl hdl)
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_action_lock);
|
std::lock_guard<std::mutex> guard(m_action_lock);
|
||||||
//std::cout << "on_open" << std::endl;
|
// std::cout << "on_open" << std::endl;
|
||||||
m_actions.push(action(SUBSCRIBE,hdl));
|
m_actions.push(action(SUBSCRIBE, hdl));
|
||||||
}
|
}
|
||||||
m_action_cond.notify_one();
|
m_action_cond.notify_one();
|
||||||
}
|
}
|
||||||
@@ -54,27 +55,31 @@ void Websocket::on_close(websocketpp::connection_hdl hdl)
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_action_lock);
|
std::lock_guard<std::mutex> guard(m_action_lock);
|
||||||
//std::cout << "on_close" << std::endl;
|
// std::cout << "on_close" << std::endl;
|
||||||
m_actions.push(action(UNSUBSCRIBE,hdl));
|
m_actions.push(action(UNSUBSCRIBE, hdl));
|
||||||
}
|
}
|
||||||
m_action_cond.notify_one();
|
m_action_cond.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Websocket::on_message(websocketpp::connection_hdl hdl, Server::message_ptr msg) {
|
void Websocket::on_message(websocketpp::connection_hdl hdl, Server::message_ptr msg)
|
||||||
|
{
|
||||||
// queue message up for sending by processing thread
|
// queue message up for sending by processing thread
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_action_lock);
|
std::lock_guard<std::mutex> guard(m_action_lock);
|
||||||
//std::cout << "on_message" << std::endl;
|
// std::cout << "on_message" << std::endl;
|
||||||
m_actions.push(action(MESSAGE,hdl,msg));
|
m_actions.push(action(MESSAGE, hdl, msg));
|
||||||
}
|
}
|
||||||
m_action_cond.notify_one();
|
m_action_cond.notify_one();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Websocket::process_messages() {
|
void Websocket::process_messages()
|
||||||
while(1) {
|
{
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
std::unique_lock<std::mutex> lock(m_action_lock);
|
std::unique_lock<std::mutex> lock(m_action_lock);
|
||||||
|
|
||||||
while(m_actions.empty()) {
|
while (m_actions.empty())
|
||||||
|
{
|
||||||
m_action_cond.wait(lock);
|
m_action_cond.wait(lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -83,38 +88,49 @@ void Websocket::process_messages() {
|
|||||||
|
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
|
|
||||||
switch (a.type)
|
switch (a.type) {
|
||||||
{
|
case SUBSCRIBE:
|
||||||
case SUBSCRIBE:
|
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_connection_lock);
|
std::lock_guard<std::mutex> guard(m_connection_lock);
|
||||||
m_connections.insert(a.hdl);
|
m_connections.insert(a.hdl);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case UNSUBSCRIBE:
|
case UNSUBSCRIBE:
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_connection_lock);
|
std::lock_guard<std::mutex> guard(m_connection_lock);
|
||||||
m_connections.erase(a.hdl);
|
m_connections.erase(a.hdl);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case MESSAGE:
|
case MESSAGE:
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_connection_lock);
|
std::lock_guard<std::mutex> guard(m_connection_lock);
|
||||||
std::string response = m_on_message(a.msg->get_payload());
|
std::string response = m_on_message(a.msg->get_payload());
|
||||||
m_server.send(a.hdl, response, websocketpp::frame::opcode::TEXT);
|
m_server.send(a.hdl, response, websocketpp::frame::opcode::TEXT);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
// undefined.
|
// undefined.
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::string update = m_on_update();
|
void Websocket::process_broadcast()
|
||||||
if (!update.empty())
|
{
|
||||||
|
while (1)
|
||||||
|
{
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> guard(m_connection_lock);
|
std::lock_guard<std::mutex> guard(m_connection_lock);
|
||||||
for (auto it = m_connections.begin(); it != m_connections.end(); ++it)
|
if (m_connections.empty())
|
||||||
{
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::string update = m_on_update();
|
||||||
|
if (!update.empty() && update != "{}")
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> guard(m_connection_lock);
|
||||||
|
for (auto it = m_connections.begin(); it != m_connections.end(); ++it) {
|
||||||
m_server.send(*it, update, websocketpp::frame::opcode::TEXT);
|
m_server.send(*it, update, websocketpp::frame::opcode::TEXT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -39,6 +39,7 @@ public:
|
|||||||
void run();
|
void run();
|
||||||
void stop();
|
void stop();
|
||||||
void process_messages();
|
void process_messages();
|
||||||
|
void process_broadcast();
|
||||||
private:
|
private:
|
||||||
void on_open(websocketpp::connection_hdl hdl);
|
void on_open(websocketpp::connection_hdl hdl);
|
||||||
void on_close(websocketpp::connection_hdl hdl);
|
void on_close(websocketpp::connection_hdl hdl);
|
||||||
|
|||||||
Reference in New Issue
Block a user