Skip to content

Add function to call websocket request from JavaScript #441

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.websocketRequest(function (response_data) {
console.log(JSON.stringify(response))
}, request_type, request_data)
```

### Register for visibility callbacks

14 changes: 7 additions & 7 deletions browser-app.cpp
Original file line number Diff line number Diff line change
@@ -99,13 +99,13 @@ void BrowserApp::OnBeforeCommandLineProcessing(const CefString &, CefRefPtr<CefC
#endif
}

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

void BrowserApp::OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame>, CefRefPtr<CefV8Context> context)
{
19 changes: 19 additions & 0 deletions browser-client.cpp
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
#include "browser-client.hpp"
#include "obs-browser-source.hpp"
#include "base64/base64.hpp"
#include <obs-websocket-api.h>
#include <nlohmann/json.hpp>
#include <obs-frontend-api.h>
#include <obs.hpp>
@@ -153,6 +154,24 @@ bool BrowserClient::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefR
obs_frontend_start_virtualcam();
} else if (name == "stopVirtualcam") {
obs_frontend_stop_virtualcam();
} else if (name == "websocketRequest") {
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);
} else {
json = {{"code", 205}, // GenericError
{"result", false},
{"comment", "Unable to call obs-websocket request"}};
}
}
[[fallthrough]];
case ControlLevel::Advanced: