Skip to content

Commit 79216de

Browse files
committed
Add function to call websocket request from JavaScript
1 parent b56fd78 commit 79216de

File tree

3 files changed

+47
-7
lines changed

3 files changed

+47
-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.websocketRequest(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
@@ -99,13 +99,13 @@ void BrowserApp::OnBeforeCommandLineProcessing(const CefString &, CefRefPtr<CefC
9999
#endif
100100
}
101101

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

110110
void BrowserApp::OnContextCreated(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame>, CefRefPtr<CefV8Context> context)
111111
{

browser-client.cpp

+15
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>
@@ -153,6 +154,20 @@ bool BrowserClient::OnProcessMessageReceived(CefRefPtr<CefBrowser> browser, CefR
153154
obs_frontend_start_virtualcam();
154155
} else if (name == "stopVirtualcam") {
155156
obs_frontend_stop_virtualcam();
157+
} else if (name == "websocketRequest") {
158+
std::string request_type = input_args->GetString(1).ToString();
159+
std::string request_data_string = input_args->GetString(2).ToString();
160+
OBSDataAutoRelease request_data = obs_data_create_from_json(request_data_string.c_str());
161+
struct obs_websocket_request_response *response =
162+
obs_websocket_call_request(request_type.c_str(), request_data);
163+
if (response) {
164+
json = {{"code", response->status_code}, {"result", response->status_code == 100}};
165+
if (response->response_data)
166+
json["responseData"] = response->response_data;
167+
if (response->comment)
168+
json["comment"] = response->comment;
169+
obs_websocket_request_response_free(response);
170+
}
156171
}
157172
[[fallthrough]];
158173
case ControlLevel::Advanced:

0 commit comments

Comments
 (0)