Getting Started - dmc-websockets

Get up-and-running quickly with dmc-websockets.

dmc-websockets is an implementation of WebSockets (RFC6455) for the Corona SDK.

sparse-docs
Since docs are sparse at this point, it'll be good to look at the page for the dmc-websockets API or run the examples included in the library at github.com.

I have listed some examples below.

dmc-websockets Basics

Usage Overview

This WebSockets implementation tries to follow the one for web browsers as closely as possible. Here are some URLs as reference:

DMC WebSockets Echo Example


--====================================================================--
-- Imports
--====================================================================--

-- local Utils = require 'dmc_corona.dmc_utils'
local WebSockets = require 'dmc_corona.dmc_websockets'


--====================================================================--
-- Setup, Constants
--====================================================================--

local ws, p


--====================================================================--
-- Support Functions
--====================================================================--


local function sendMessages()

    local num_msgs = 5
    local count = 0

    print("=== Sending " .. tostring( num_msgs ) .. " messages ===\n")

    local f = function()

        count = count + 1
        local str = "Current app time: " .. tostring( system.getTimer() )
        print( "Sending message (" .. tostring( count ) .. "): '" .. str .. "'")

        -- ws:_sendPing()
        ws:send( str )

        if count == num_msgs then
            timer.performWithDelay( 1500, function() print( "Closing WebSocket connection" ); ws:close() end )
        end

    end

    timer.performWithDelay( 1000, f, num_msgs )

end


--====================================================================--
-- Main Functions
--====================================================================--

local function webSocketsEvent_handler( event )

    local evt_type = event.type

    if evt_type == ws.ONOPEN then
        print( 'Received event: ONOPEN' )
        sendMessages()

    elseif evt_type == ws.ONMESSAGE then
        local msg = event.message

        print( "Received event: ONMESSAGE" )
        print( "echoed message: '" .. tostring( msg.data ) .. "'\n\n" )

    elseif evt_type == ws.ONCLOSE then
        print( "Received event: ONCLOSE" )

    elseif evt_type == ws.ONERROR then
        print( "Received event: ONERROR" )
        -- Utils.print( event )

    end
end

p = {
    uri='ws://echo.websocket.org'
}
ws = WebSockets:new( p )
ws:addEventListener( ws.EVENT, webSocketsEvent_handler )

dmc-websockets

Output

dmc_websockets:: Connecting to 'echo.websocket.org'
dmc_websockets:: Sending WebSocket request to server
dmc_websockets:: Reading WebSocket response from server
dmc_websockets:: Connected to server
Received event: ONOPEN
=== Sending 5 messages ===
Sending message (1): 'Current app time: 1676.4'
Received event: ONMESSAGE
echoed message: 'Current app time: 1676.4'

Sending message (2): 'Current app time: 2699.506'
Received event: ONMESSAGE
echoed message: 'Current app time: 2699.506'

Sending message (3): 'Current app time: 3722.425'
Received event: ONMESSAGE
echoed message: 'Current app time: 3722.425'

Sending message (4): 'Current app time: 4745.483'
Received event: ONMESSAGE
echoed message: 'Current app time: 4745.483'

Sending message (5): 'Current app time: 5767.712'
Received event: ONMESSAGE
echoed message: 'Current app time: 5767.712'

Closing WebSocket connection
dmc_websockets:: Server connection closed
Received event: ONCLOSE

Huge thanks to these projects: