mirror of
https://github.com/Palakis/obs-websocket.git
synced 2024-08-30 18:12:16 +00:00
OBSRemoteProtocol: refactor class + pass handler when calling processMessage
This commit is contained in:
parent
5e393ed3c4
commit
208c7f18b2
@ -177,8 +177,8 @@ void WSServer::onMessage(connection_hdl hdl, server::message_ptr message)
|
|||||||
}
|
}
|
||||||
|
|
||||||
WSRequestHandler requestHandler(connProperties);
|
WSRequestHandler requestHandler(connProperties);
|
||||||
OBSRemoteProtocol protocol(requestHandler);
|
OBSRemoteProtocol protocol;
|
||||||
std::string response = protocol.processMessage(payload);
|
std::string response = protocol.processMessage(requestHandler, payload);
|
||||||
|
|
||||||
if (GetConfig()->DebugEnabled) {
|
if (GetConfig()->DebugEnabled) {
|
||||||
blog(LOG_INFO, "Response << '%s'", response.c_str());
|
blog(LOG_INFO, "Response << '%s'", response.c_str());
|
||||||
|
@ -19,35 +19,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
|
|||||||
#include "OBSRemoteProtocol.h"
|
#include "OBSRemoteProtocol.h"
|
||||||
#include "../WSRequestHandler.h"
|
#include "../WSRequestHandler.h"
|
||||||
|
|
||||||
std::string buildResponse(QString messageId, QString status, obs_data_t* fields = nullptr) {
|
std::string OBSRemoteProtocol::processMessage(WSRequestHandler& requestHandler, std::string message)
|
||||||
OBSDataAutoRelease response = obs_data_create();
|
|
||||||
obs_data_set_string(response, "message-id", messageId.toUtf8().constData());
|
|
||||||
obs_data_set_string(response, "status", status.toUtf8().constData());
|
|
||||||
|
|
||||||
if (fields) {
|
|
||||||
obs_data_apply(response, fields);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string responseString = obs_data_get_json(response);
|
|
||||||
return responseString;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string successResponse(QString messageId, obs_data_t* fields = nullptr) {
|
|
||||||
return buildResponse(messageId, "ok", fields);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string errorResponse(QString messageId, QString errorMessage, obs_data_t* additionalFields = nullptr) {
|
|
||||||
OBSDataAutoRelease fields = obs_data_create();
|
|
||||||
obs_data_set_string(fields, "error", errorMessage.toUtf8().constData());
|
|
||||||
return buildResponse(messageId, "error", fields);
|
|
||||||
}
|
|
||||||
|
|
||||||
OBSRemoteProtocol::OBSRemoteProtocol(WSRequestHandler& requestHandler) :
|
|
||||||
_requestHandler(requestHandler)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
std::string OBSRemoteProtocol::processMessage(std::string message)
|
|
||||||
{
|
{
|
||||||
std::string msgContainer(message);
|
std::string msgContainer(message);
|
||||||
const char* msg = msgContainer.c_str();
|
const char* msg = msgContainer.c_str();
|
||||||
@ -71,7 +43,7 @@ std::string OBSRemoteProtocol::processMessage(std::string message)
|
|||||||
obs_data_unset_user_value(params, "message-id");
|
obs_data_unset_user_value(params, "message-id");
|
||||||
|
|
||||||
RpcRequest request(messageId, methodName, params);
|
RpcRequest request(messageId, methodName, params);
|
||||||
RpcResponse response = _requestHandler.processRequest(request);
|
RpcResponse response = requestHandler.processRequest(request);
|
||||||
|
|
||||||
OBSData additionalFields = response.additionalFields();
|
OBSData additionalFields = response.additionalFields();
|
||||||
switch (response.status()) {
|
switch (response.status()) {
|
||||||
@ -83,3 +55,26 @@ std::string OBSRemoteProtocol::processMessage(std::string message)
|
|||||||
|
|
||||||
return std::string();
|
return std::string();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string OBSRemoteProtocol::buildResponse(QString messageId, QString status, obs_data_t* fields) {
|
||||||
|
OBSDataAutoRelease response = obs_data_create();
|
||||||
|
obs_data_set_string(response, "message-id", messageId.toUtf8().constData());
|
||||||
|
obs_data_set_string(response, "status", status.toUtf8().constData());
|
||||||
|
|
||||||
|
if (fields) {
|
||||||
|
obs_data_apply(response, fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string responseString = obs_data_get_json(response);
|
||||||
|
return responseString;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string OBSRemoteProtocol::successResponse(QString messageId, obs_data_t* fields) {
|
||||||
|
return buildResponse(messageId, "ok", fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string OBSRemoteProtocol::errorResponse(QString messageId, QString errorMessage, obs_data_t* additionalFields) {
|
||||||
|
OBSDataAutoRelease fields = obs_data_create();
|
||||||
|
obs_data_set_string(fields, "error", errorMessage.toUtf8().constData());
|
||||||
|
return buildResponse(messageId, "error", fields);
|
||||||
|
}
|
||||||
|
@ -20,15 +20,17 @@ with this program. If not, see <https://www.gnu.org/licenses/>
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <obs-data.h>
|
#include <obs-data.h>
|
||||||
|
#include <QtCore/QString>
|
||||||
|
|
||||||
class WSRequestHandler;
|
class WSRequestHandler;
|
||||||
|
|
||||||
class OBSRemoteProtocol
|
class OBSRemoteProtocol
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
explicit OBSRemoteProtocol(WSRequestHandler& requestHandler);
|
std::string processMessage(WSRequestHandler& requestHandler, std::string message);
|
||||||
std::string processMessage(std::string message);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
WSRequestHandler& _requestHandler;
|
std::string buildResponse(QString messageId, QString status, obs_data_t* fields = nullptr);
|
||||||
|
std::string successResponse(QString messageId, obs_data_t* fields = nullptr);
|
||||||
|
std::string errorResponse(QString messageId, QString errorMessage, obs_data_t* additionalFields = nullptr);
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user