Skip to content
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

Add function to call websocket request from JavaScript #441

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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