Skip to content

Commit aae88c8

Browse files
committed
Add function to call websocket request from JavaScript
1 parent 996b5a7 commit aae88c8

File tree

3 files changed

+55
-7
lines changed

3 files changed

+55
-7
lines changed

README.md

+25
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,31 @@ Permissions required: ALL
315315
window.obsstudio.stopVirtualcam()
316316
```
317317

318+
#### Call Websocket request
319+
Permissions required: ALL
320+
```js
321+
/**
322+
* @typedef {Object} WebsocketResponse
323+
* @property {number} code - RequestStatus code
324+
* @property {bool} result - is true if the request resulted in Success. False if otherwise.
325+
* @property {string} responseData - JSON string containing response data
326+
* @property {string} comment - may be provided by the server on errors to offer further details on why a request failed.
327+
*/
328+
329+
/**
330+
* @callback WebsocketRequestCallback
331+
* @param {WebsocketResponse} response
332+
*/
333+
334+
/**
335+
* @param {WebsocketRequestCallback} cb
336+
* @param {string} request_type - The request type to call
337+
* @param {string} request_data - JSON string containing appropriate request data
338+
*/
339+
window.obsstudio.callWebsocketRequest(function (response_data) {
340+
console.log(JSON.stringify(response))
341+
}, request_type, request_data)
342+
```
318343

319344
### Register for visibility callbacks
320345

browser-app.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ void BrowserApp::OnBeforeCommandLineProcessing(
100100
}
101101

102102
std::vector<std::string> exposedFunctions = {
103-
"getControlLevel", "getCurrentScene", "getStatus",
104-
"startRecording", "stopRecording", "startStreaming",
105-
"stopStreaming", "pauseRecording", "unpauseRecording",
106-
"startReplayBuffer", "stopReplayBuffer", "saveReplayBuffer",
107-
"startVirtualcam", "stopVirtualcam", "getScenes",
108-
"setCurrentScene", "getTransitions", "getCurrentTransition",
109-
"setCurrentTransition"};
103+
"getControlLevel", "getCurrentScene", "getStatus",
104+
"startRecording", "stopRecording", "startStreaming",
105+
"stopStreaming", "pauseRecording", "unpauseRecording",
106+
"startReplayBuffer", "stopReplayBuffer", "saveReplayBuffer",
107+
"startVirtualcam", "stopVirtualcam", "getScenes",
108+
"setCurrentScene", "getTransitions", "getCurrentTransition",
109+
"setCurrentTransition", "callWebsocketRequest"};
110110

111111
void BrowserApp::OnContextCreated(CefRefPtr<CefBrowser> browser,
112112
CefRefPtr<CefFrame>,

browser-client.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "browser-client.hpp"
2020
#include "obs-browser-source.hpp"
2121
#include "base64/base64.hpp"
22+
#include "..\obs-websocket\lib\obs-websocket-api.h"
2223
#include <nlohmann/json.hpp>
2324
#include <obs-frontend-api.h>
2425
#include <obs.hpp>
@@ -142,6 +143,28 @@ bool BrowserClient::OnProcessMessageReceived(
142143
obs_frontend_start_virtualcam();
143144
} else if (name == "stopVirtualcam") {
144145
obs_frontend_stop_virtualcam();
146+
} else if (name == "callWebsocketRequest") {
147+
std::string request_type =
148+
input_args->GetString(1).ToString();
149+
std::string request_data_string =
150+
input_args->GetString(2).ToString();
151+
OBSDataAutoRelease request_data =
152+
obs_data_create_from_json(
153+
request_data_string.c_str());
154+
struct obs_websocket_request_response *response =
155+
obs_websocket_call_request(request_type.c_str(),
156+
request_data);
157+
if (response) {
158+
json = {{"code", response->status_code},
159+
{"result",
160+
response->status_code == 100}};
161+
if (response->response_data)
162+
json["responseData"] =
163+
response->response_data;
164+
if (response->comment)
165+
json["comment"] = response->comment;
166+
obs_websocket_request_response_free(response);
167+
}
145168
}
146169
[[fallthrough]];
147170
case ControlLevel::Advanced:

0 commit comments

Comments
 (0)