mirror of
https://github.com/Sani7/remote-ui.git
synced 2026-10-27 06:01:14 +01:00
Move to Qt
Everything seems to be working Only timer is not moved over
This commit is contained in:
+24
-60
@@ -1,69 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <functional>
|
||||
#include <set>
|
||||
#include <QtCore/QObject>
|
||||
#include <QtCore/QList>
|
||||
#include <QtCore/QByteArray>
|
||||
|
||||
#include <websocketpp/config/asio_no_tls.hpp>
|
||||
#include <websocketpp/server.hpp>
|
||||
QT_FORWARD_DECLARE_CLASS(QWebSocketServer)
|
||||
QT_FORWARD_DECLARE_CLASS(QWebSocket)
|
||||
|
||||
typedef websocketpp::server<websocketpp::config::asio> Server;
|
||||
|
||||
using websocketpp::lib::placeholders::_1;
|
||||
using websocketpp::lib::placeholders::_2;
|
||||
|
||||
/* on_open insert connection_hdl into channel
|
||||
* on_close remove connection_hdl from channel
|
||||
* on_message queue send to all channels
|
||||
*/
|
||||
|
||||
enum action_type
|
||||
class Websocket : public QObject
|
||||
{
|
||||
SUBSCRIBE,
|
||||
UNSUBSCRIBE,
|
||||
MESSAGE,
|
||||
BROADCAST
|
||||
};
|
||||
Q_OBJECT
|
||||
|
||||
struct action
|
||||
{
|
||||
action(action_type t, websocketpp::connection_hdl h) : type(t), hdl(h)
|
||||
{
|
||||
}
|
||||
action(action_type t, websocketpp::connection_hdl h, Server::message_ptr m) : type(t), hdl(h), msg(m)
|
||||
{
|
||||
}
|
||||
|
||||
action_type type;
|
||||
websocketpp::connection_hdl hdl;
|
||||
Server::message_ptr msg;
|
||||
};
|
||||
|
||||
class Websocket
|
||||
{
|
||||
public:
|
||||
Websocket(uint16_t port, std::ostream *out, std::ostream *err, std::function<std::string(std::string)> on_message,
|
||||
std::function<std::string()> on_update);
|
||||
void run();
|
||||
void stop();
|
||||
void process_messages();
|
||||
void process_broadcast();
|
||||
Websocket(uint16_t port, QObject *parent = nullptr);
|
||||
~Websocket();
|
||||
|
||||
void broadcast(QString message);
|
||||
void send(QWebSocket* conn, QString message);
|
||||
signals:
|
||||
void on_message(QWebSocket* conn, QString message);
|
||||
void closed();
|
||||
|
||||
private slots:
|
||||
void onNewConnection();
|
||||
void processTextMessage(QString message);
|
||||
void processBinaryMessage(QByteArray message);
|
||||
void socketDisconnected();
|
||||
|
||||
private:
|
||||
void on_open(websocketpp::connection_hdl hdl);
|
||||
void on_close(websocketpp::connection_hdl hdl);
|
||||
void on_message(websocketpp::connection_hdl hdl, Server::message_ptr msg);
|
||||
|
||||
private:
|
||||
std::function<std::string(std::string)> m_on_message;
|
||||
std::function<std::string()> m_on_update;
|
||||
Server m_server;
|
||||
uint16_t m_port;
|
||||
typedef std::set<websocketpp::connection_hdl, std::owner_less<websocketpp::connection_hdl>> con_list;
|
||||
con_list m_connections;
|
||||
|
||||
std::queue<action> m_actions;
|
||||
|
||||
std::mutex m_action_lock;
|
||||
std::mutex m_connection_lock;
|
||||
std::condition_variable m_action_cond;
|
||||
};
|
||||
QWebSocketServer *m_pWebSocketServer;
|
||||
QList<QWebSocket *> m_clients;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user