mirror of
https://github.com/Palakis/obs-websocket.git
synced 2024-08-30 18:12:16 +00:00
server: return refactor
This commit is contained in:
parent
c074088f2f
commit
fe1b14ff57
@ -135,18 +135,15 @@ WSRequestHandler::WSRequestHandler() :
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
void WSRequestHandler::processIncomingMessage(QString textMessage) {
|
std::string WSRequestHandler::processIncomingMessage(std::string& textMessage) {
|
||||||
QByteArray msgData = textMessage.toUtf8();
|
std::string msgContainer(textMessage);
|
||||||
const char* msg = msgData.constData();
|
const char* msg = msgContainer.c_str();
|
||||||
|
|
||||||
data = obs_data_create_from_json(msg);
|
data = obs_data_create_from_json(msg);
|
||||||
if (!data) {
|
if (!data) {
|
||||||
if (!msg)
|
|
||||||
msg = "<null pointer>";
|
|
||||||
|
|
||||||
blog(LOG_ERROR, "invalid JSON payload received for '%s'", msg);
|
blog(LOG_ERROR, "invalid JSON payload received for '%s'", msg);
|
||||||
SendErrorResponse("invalid JSON payload");
|
SendErrorResponse("invalid JSON payload");
|
||||||
return;
|
return _response;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Config::Current()->DebugEnabled) {
|
if (Config::Current()->DebugEnabled) {
|
||||||
@ -157,7 +154,7 @@ void WSRequestHandler::processIncomingMessage(QString textMessage) {
|
|||||||
|| !hasField("message-id"))
|
|| !hasField("message-id"))
|
||||||
{
|
{
|
||||||
SendErrorResponse("missing request parameters");
|
SendErrorResponse("missing request parameters");
|
||||||
return;
|
return _response;
|
||||||
}
|
}
|
||||||
|
|
||||||
_requestType = obs_data_get_string(data, "request-type");
|
_requestType = obs_data_get_string(data, "request-type");
|
||||||
@ -177,6 +174,8 @@ void WSRequestHandler::processIncomingMessage(QString textMessage) {
|
|||||||
handlerFunc(this);
|
handlerFunc(this);
|
||||||
else
|
else
|
||||||
SendErrorResponse("invalid request type");
|
SendErrorResponse("invalid request type");
|
||||||
|
|
||||||
|
return _response;
|
||||||
}
|
}
|
||||||
|
|
||||||
WSRequestHandler::~WSRequestHandler() {
|
WSRequestHandler::~WSRequestHandler() {
|
||||||
@ -214,11 +213,10 @@ void WSRequestHandler::SendErrorResponse(obs_data_t* additionalFields) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void WSRequestHandler::SendResponse(obs_data_t* response) {
|
void WSRequestHandler::SendResponse(obs_data_t* response) {
|
||||||
assert(_response.isNull());
|
|
||||||
_response = obs_data_get_json(response);
|
_response = obs_data_get_json(response);
|
||||||
|
|
||||||
if (Config::Current()->DebugEnabled)
|
if (Config::Current()->DebugEnabled)
|
||||||
blog(LOG_DEBUG, "Response << '%s'", _response.toUtf8().constData());
|
blog(LOG_DEBUG, "Response << '%s'", _response.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool WSRequestHandler::hasField(QString name) {
|
bool WSRequestHandler::hasField(QString name) {
|
||||||
|
@ -34,16 +34,13 @@ class WSRequestHandler : public QObject {
|
|||||||
public:
|
public:
|
||||||
explicit WSRequestHandler();
|
explicit WSRequestHandler();
|
||||||
~WSRequestHandler();
|
~WSRequestHandler();
|
||||||
void processIncomingMessage(QString textMessage);
|
std::string processIncomingMessage(std::string& textMessage);
|
||||||
bool hasField(QString name);
|
bool hasField(QString name);
|
||||||
QString getResponse() {
|
|
||||||
return _response;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const char* _messageId;
|
const char* _messageId;
|
||||||
const char* _requestType;
|
const char* _requestType;
|
||||||
QString _response;
|
std::string _response;
|
||||||
OBSDataAutoRelease data;
|
OBSDataAutoRelease data;
|
||||||
|
|
||||||
void SendOKResponse(obs_data_t* additionalFields = NULL);
|
void SendOKResponse(obs_data_t* additionalFields = NULL);
|
||||||
|
@ -118,12 +118,10 @@ void WSServer::onMessage(connection_hdl hdl, server::message_ptr message)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
QString payload = QString::fromStdString(message->get_payload());
|
std::string payload = message->get_payload();
|
||||||
|
|
||||||
// TODO refactor handler
|
|
||||||
WSRequestHandler handler;
|
WSRequestHandler handler;
|
||||||
handler.processIncomingMessage(payload);
|
std::string response = handler.processIncomingMessage(payload);
|
||||||
std::string response = handler.getResponse().toStdString();
|
|
||||||
|
|
||||||
_server.send(hdl, response, websocketpp::frame::opcode::text);
|
_server.send(hdl, response, websocketpp::frame::opcode::text);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user