sha256_base64

API -

return function()
    local app = getApp();
    local state = app.getAppState();
    
    --encryption and base64 shortcut
    local sha256AndBase64 = app.sha256_base64('somestring');
    log(sha256AndBase64);
end

This is usually used with security issues and dealing with API stuff.

Example script for OBS websockets

function authentication(ob)
    local challenge = ob['d']['authentication']['challenge'];
    local salt = ob['d']['authentication']['salt'];

    set('rpcVersion', ob['d']['authentication']['rpcVersion'])

    local app = getApp();
    local salted_sha_base64 = app.sha256_base64(password .. salt); 
    local password_challenge_sha_base64 = app.sha256_base64(salted_sha_base64 .. challenge); 

    local m_data = {};
    m_data['op'] = 1;
    m_data['d'] = {};

    m_data['d']['rpcVersion'] = rpcVersion;
    m_data['d']['authentication'] = password_challenge_sha_base64;
    m_data['d']['eventSubscriptions'] = 33; --these are events you can subscribe to

    app.sendWebsocketMessage(socket, json.serialize(m_data));

end

function yourEvent(title, type, message, code)

    if title ~= socket then --make sure we're using the socket title we want!
        return; --otherwise exit out early :)
    end

    if type == 'OnMessage' then
        --route all messages here!
        --the code will be blank. ''

        log('receiving message: ' .. message);

        local ob = json.parse(message);
        if ob['op'] == 0 then
            authentication(ob);
        end

        if ob['op'] == 2 then
           switchToScene(gamePlaySceneName);
        end
    end
    if type == 'OnOpen' then
        --the socket opened!!
        --the message and code will be blank. ''
        log('socket was opened!');
    end

    if type == 'OnClose' or type == 'OnError' then
        --if the message type is OnClose or OnError, let's clean up the socket...
        local app = getApp();
        app.removeWebSocket(socket);
        log('socket is closed!');
        log(message);
        log(code);
    end
end


function switchToScene(scene)
    local m_data = {};
    m_data['op'] = 6;
    m_data['d'] = {};
    local app = getApp();
    m_data['d']['requestType'] = 'SetCurrentProgramScene';
    m_data['d']['requestId'] = 'f819dcf0-89cc-11eb-8f0e-382c4ac93b9c';
    m_data['d']['requestData'] = {};
    m_data['d']['requestData']['sceneName'] = scene;
    app.sendWebsocketMessage(socket, json.serialize(m_data));
end

--retrieved from json data
ip = 'ws://192.168.1.70:4444';
password = 'your_websocket_password_in_obs';
gamePlaySceneName = 'your scene title';
--
rpcVersion = 1;
socket = 'obs_socket_holder'; 

return function()

    load();
    log('OBS websocket tutorial');
    local app = getApp();

    --remove old existing websocket just incase...
    app.removeWebSocket(socket);
    wait(1); --give it time to remove the old one
    addEvent('websocket', 'yourEvent'); --subscribe to all websockets that exist...

    
    local params = {};
    params[1] = 'Sec-WebSocket-Protocol';
    params[2] = 'obswebsocket.json';
    app.createWebsocket(socket, ip, params); --title a websoscket and connect to a server...

    keepAlive();
end

Last updated