@duncte123/obs-websocket-js
Version:
OBS Websocket API in Javascript, consumes @Palakis/obs-websocket
101 lines (100 loc) • 4.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.OBSWebSocket = void 0;
const Socket_js_1 = require("./Socket.js");
const Status_js_1 = require("./Status.js");
class OBSWebSocket extends Socket_js_1.Socket {
static generateMessageId() {
return String(OBSWebSocket.requestCounter++);
}
/**
* Generic Socket request method. Returns a promise.
* Generates a messageId internally and will override any passed in the args.
* Note that the requestType here is pre-marshaling and currently must match exactly what the websocket plugin is expecting.
*
* @param {String} requestType obs-websocket plugin expected request type.
* @param {Object} [args={}] request arguments.
* @return {Promise} Promise, passes the plugin response object.
*/
send(requestType, args) {
// @ts-ignore this assignment works in js
// eslint-disable-next-line no-param-reassign
args = args || {};
return new Promise((resolve, reject) => {
const messageId = OBSWebSocket.generateMessageId();
let rejectReason = null;
if (!requestType) {
rejectReason = Status_js_1.Status.REQUEST_TYPE_NOT_SPECIFIED;
}
if (args && (typeof args !== 'object' || Array.isArray(args))) {
rejectReason = Status_js_1.Status.ARGS_NOT_OBJECT;
}
if (!this.connected) {
rejectReason = Status_js_1.Status.NOT_CONNECTED;
}
// Assign a temporary event listener for this particular messageId to uniquely identify the response.
this.once(`obs:internal:message:id-${messageId}`, (err, data) => {
if (err && Object.keys(err).length > 0) {
this.debug('[send:reject] %o', err);
reject(err);
}
else {
this.debug('[send:resolve] %o', data);
resolve(data);
}
});
// If we don't have a reason to fail fast, send the request to the socket.
if (!rejectReason) {
// @ts-ignore not documented but required
args['request-type'] = requestType;
// @ts-ignore not documented but required
args['message-id'] = messageId;
// Submit the request to the websocket.
this.debug('[send] %s %s %o', messageId, requestType, args);
try {
this.socket.send(JSON.stringify(args));
}
catch (_) {
// TODO: Consider inspecting the exception thrown to gleam some relevant info and pass that on.
rejectReason = Status_js_1.Status.SOCKET_EXCEPTION;
}
}
// If the socket call was unsuccessful or bypassed, simulate its resolution.
if (rejectReason) {
this.emit(`obs:internal:message:id-${messageId}`, rejectReason);
}
});
}
/**
* Generic Socket request method. Handles callbacks.
* Internally calls `send` (which is promise-based). See `send`'s docs for more details.
*
* @param {String} requestType obs-websocket plugin expected request type.
* @param {Object} [args={}] request arguments.
* @param {Function} callback Optional. callback(err, data)
* @deprecated This method is not fun to maintain in typescript, use the promise api instead
*/
// this is hell to maintain in typescript and will be removed
sendCallback(requestType, args, callback) {
// Allow the `args` argument to be omitted.
if (typeof callback === 'undefined' && typeof args === 'function') {
// eslint-disable-next-line no-param-reassign
callback = args;
// @ts-ignore this is valid
// eslint-disable-next-line no-param-reassign
args = {};
}
// Perform the actual request, using `send`.
// @ts-ignore args is stupid
this.send(requestType, args).then((...response) => {
// @ts-ignore is not undefined
callback(null, ...response);
})
.catch((error) => {
// @ts-ignore is not undefined
callback(error);
});
}
}
exports.OBSWebSocket = OBSWebSocket;
OBSWebSocket.requestCounter = 0;