createWebsocket

API -

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);
    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

return function()
    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...
    
    --this wss server will echo whatever you send!
    local protocols = { 'headerKey', 'headerValue', 'headerKey2', 'headerValue2' };
    app.createWebsocket(socket, 'wss://ws.ifelse.io/', protocols);
    keepAlive();
end

This is designed to be used with applyImage()...

applyImage

Last updated