Skip to content

Commit

Permalink
Add function to call websocket request from JavaScript
Browse files Browse the repository at this point in the history
  • Loading branch information
exeldro committed Apr 22, 2024
1 parent 996b5a7 commit aae88c8
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,31 @@ Permissions required: ALL
window.obsstudio.stopVirtualcam()
```

#### Call Websocket request
Permissions required: ALL
```js
/**
* @typedef {Object} WebsocketResponse
* @property {number} code - RequestStatus code
* @property {bool} result - is true if the request resulted in Success. False if otherwise.
* @property {string} responseData - JSON string containing response data
* @property {string} comment - may be provided by the server on errors to offer further details on why a request failed.
*/

/**
* @callback WebsocketRequestCallback
* @param {WebsocketResponse} response
*/

/**
* @param {WebsocketRequestCallback} cb
* @param {string} request_type - The request type to call
* @param {string} request_data - JSON string containing appropriate request data
*/
window.obsstudio.callWebsocketRequest(function (response_data) {
console.log(JSON.stringify(response))
}, request_type, request_data)
```

### Register for visibility callbacks

Expand Down
14 changes: 7 additions & 7 deletions browser-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,13 @@ void BrowserApp::OnBeforeCommandLineProcessing(
}

std::vector<std::string> exposedFunctions = {
"getControlLevel", "getCurrentScene", "getStatus",
"startRecording", "stopRecording", "startStreaming",
"stopStreaming", "pauseRecording", "unpauseRecording",
"startReplayBuffer", "stopReplayBuffer", "saveReplayBuffer",
"startVirtualcam", "stopVirtualcam", "getScenes",
"setCurrentScene", "getTransitions", "getCurrentTransition",
"setCurrentTransition"};
"getControlLevel", "getCurrentScene", "getStatus",
"startRecording", "stopRecording", "startStreaming",
"stopStreaming", "pauseRecording", "unpauseRecording",
"startReplayBuffer", "stopReplayBuffer", "saveReplayBuffer",
"startVirtualcam", "stopVirtualcam", "getScenes",
"setCurrentScene", "getTransitions", "getCurrentTransition",
"setCurrentTransition", "callWebsocketRequest"};

void BrowserApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame>,
Expand Down
23 changes: 23 additions & 0 deletions browser-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "browser-client.hpp"
#include "obs-browser-source.hpp"
#include "base64/base64.hpp"
#include "..\obs-websocket\lib\obs-websocket-api.h"
#include <nlohmann/json.hpp>
#include <obs-frontend-api.h>
#include <obs.hpp>
Expand Down Expand Up @@ -142,6 +143,28 @@ bool BrowserClient::OnProcessMessageReceived(
obs_frontend_start_virtualcam();
} else if (name == "stopVirtualcam") {
obs_frontend_stop_virtualcam();
} else if (name == "callWebsocketRequest") {
std::string request_type =
input_args->GetString(1).ToString();
std::string request_data_string =
input_args->GetString(2).ToString();
OBSDataAutoRelease request_data =
obs_data_create_from_json(
request_data_string.c_str());
struct obs_websocket_request_response *response =
obs_websocket_call_request(request_type.c_str(),
request_data);
if (response) {
json = {{"code", response->status_code},
{"result",
response->status_code == 100}};
if (response->response_data)
json["responseData"] =
response->response_data;
if (response->comment)
json["comment"] = response->comment;
obs_websocket_request_response_free(response);
}
}
[[fallthrough]];
case ControlLevel::Advanced:
Expand Down

0 comments on commit aae88c8

Please sign in to comment.