Getting Started with dmc-sockets

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

dmc-sockets is an easy-to-use, non-blocking socket library. Currently there are two different TCP sockets available — TCP and Async-TCP — depending on your needs.

Table of Contents

{toc:style=disc|indent=20px|minLevel=2|exclude=Table of Contents|printable=false}

dmc-sockets Basics

Usage Overview

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

I have listed the examples below.

Basic TCP Socket Example

The output is at the end of the code listing.

{code:language=none|title=DMC Sockets TCP Basic Example}

-- Imports

local Sockets = require 'dmc_library.dmc_sockets'

--====================================================================--

-- Setup, Constants

local host, port = 'docs.davidmccuskey.com', 80 local sock

--====================================================================--

-- Support Functions

-- make up a generic request for the web server

local function sendRequest()

local bytes, err
local req_t, req

local req_t = {
    "GET / HTTP/1.1\r\n",
    "Host: " .. host .. "\r\n",
    "User-Agent: DMC Sockets " .. Sockets.VERSION .. "\r\n",
    "\r\n"
}
req = table.concat( req_t, "" )

bytes, err = sock:send( req )

end

-- our socket callback function

local function socketCallback( event ) -- print( "socketCallback", event.type )

local status = event.status

if event.type == sock.CONNECT then

    if status == sock.CONNECTED then
        print( "=== Socket Connected ===" )
        print( 'socket status:', status )

        sendRequest()

    else
        print( "=== Socket Disconnected ===" )
        print( 'socket status:', status )

        timer.performWithDelay( 2000, function() sock:reconnect() end )
    end


elseif event.type == sock.READ then
    local len = event.bytes

    print( "=== Data Available ===" )
    print( 'socket status:', status )

    print( 'Before read (buff len):', len )

    -- read our data, using one of several receive types
    repeat
        local data = sock:receive( '*l' )
        print( '>>', data )
    until data == ''

    print( 'After read (buff len):', sock.buffer_size )

end

end

--====================================================================--

-- Main

sock = Sockets:create( Sockets.TCP ) sock:addEventListener( sock.EVENT, socketCallback ) sock:connect( host, port )


{code:language=none|title=Output}
=== Socket Connected ===
socket status:    socket_connected
=== Data Available ===
socket status:    socket_connected
Before read (buff len):    nil
>>    HTTP/1.1 302 Moved Temporarily
>>    Server: nginx
>>    Date: Mon, 12 May 2014 18:46:27 GMT
>>    Content-Type: text/html
>>    Content-Length: 0
>>    Connection: keep-alive
>>    Set-Cookie: JSESSIONID=341FD6AFE733D0F418FEA47840CB2E9F; Path=/; HttpOnly
>>    Location: http://docs.davidmccuskey.com/homepage.action
>>
After read (buff len):    0


=== Socket Disconnected ===
socket status:    socket_closed
=== Socket Connected ===

< ... this is repeated ... >

Basic Async-TCP Socket Example

The output is at the end of the code listing.

{code:language=none|title=DMC Sockets Async-TCP Basic Example}

-- Imports

local Sockets = require 'dmc_library.dmc_sockets'

--====================================================================--

-- Setup, Constants

local host, port = 'docs.davidmccuskey.com', 80 local sock

--====================================================================--

-- Support Functions

-- make up a generic request for the web server

local function sendRequest()

local req_t, req
local bytes, err

local req_t = {
    "GET / HTTP/1.1\r\n",
    "Host: " .. host .. "\r\n",
    "User-Agent: DMC Sockets " .. Sockets.VERSION .. "\r\n",
    "\r\n"
}
req = table.concat( req_t, "" )
bytes, err = sock:send( req )

end

-- to show non-blocking aspects local function startObjectMotion()

local leftX, rightX = 10, 300
local TIME = 2000

local o = display.newRect( leftX, 200, 20, 20 )
local moveRight, moveLeft

moveRight = function()
    transition.to( o, { x=rightX, time=TIME, onComplete=moveLeft } )
end

moveLeft = function()
transition.to( o, { x=leftX, time=TIME, onComplete=moveRight } )
end

moveRight()

end

--====================================================================--

-- Main

local function main()

local onConnect, dataCallback, newlineCallback

onConnect = function( event )
    if event.status == sock.CONNECTED then
        print("=== Connection Established ===")

        sendRequest()

        print("Reading Data\n\n")
        -- sock:receive( '*l', dataCallback )
        sock:receiveUntilNewline( newlineCallback )

    elseif event.status == sock.CLOSED then
        print("=== Connection Closed ===\n\n")

        timer.performWithDelay( 4000, function() print("Reconnecting\n\n") ; sock:reconnect( { onConnect=onConnect } ) end )

    else
        print("=== Connection Error ===")
        print( event.emsg )

    end
end

dataCallback = function( event )
    print("== Data Handler ==")
    print( '>>', event.data, event.emsg )
end

newlineCallback = function( event )
    print("== Newline Handler ==")
    event = event or {}

    if not event.data then
        print( event.emsg )

    else
        print("Received Data:\n")
        for i,v in ipairs( event.data ) do
            print(i,v)
        end
        print("\n")

    end
end

print("Starting Connection")
sock = Sockets:create( Sockets.ATCP )
sock:connect( host, port, { onConnect=onConnect } )

end

main() startObjectMotion()


{code:language=none|title=Output}

Starting Connection
=== Connection Established ===
Reading Data

== Newline Handler ==
Received Data:
1    HTTP/1.1 302 Moved Temporarily
2    Server: nginx
3    Date: Mon, 12 May 2014 18:44:37 GMT
4    Content-Type: text/html
5    Content-Length: 0
6    Connection: keep-alive
7    Set-Cookie: JSESSIONID=6E4C8E25C8C5B27233BE0F03D43B6A86; Path=/; HttpOnly
8    Location: http://docs.davidmccuskey.com/homepage.action
9

=== Connection Closed ===

Reconnecting

=== Connection Established ===

< ... this is repeated ... >