Table of Contents

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

Sockets Manager API

Properties

throttle (milliseconds)

This controls how often the library will check for data. There are several constants available for usage: OFF, LOW, MEDIUM, HIGH. Defaults to MEDIUM.

{code:language=none|title=main.lua} local Sockets = require 'dmc_library.dmc_sockets' Sockets.throttle = Sockets.OFF

or

local Sockets = require 'dmc_library.dmc_sockets' Sockets.throttle = 500




#### check_reads (boolean)

Whether to check socket read availability. Defaults to `true`.

{code:language=none|title=main.lua}
local Sockets = require 'dmc_library.dmc_sockets'
Sockets.check_reads = false

check_writes (boolean)

Whether to check socket write availability. Defaults to false.

{code:language=none|title=main.lua} local Sockets = require 'dmc_library.dmc_sockets' Sockets.check_writes = true





### Methods


#### Sockets:create( socket_type )

{code:language=none|title=main.lua}
local Sockets = require 'dmc_library.dmc_sockets'

local socket = Sockets:create( Sockets.TCP )

local async_socket = Sockets:create( Sockets.ATCP )

Sockets Configuration File

coming soon

{anchor:tcp_socket_connection}

TCP Socket API

This is the API for the individual socket

Properties

socket.status (constant)

The current status of the raw socket connection. One of:

NO_SOCKET, NOT_CONNECTED, CONNECTED, CLOSED

{code:language=none|title=main.lua} local Sockets = require 'dmc_library.dmc_sockets'

local socket = Sockets:create( Sockets.TCP )

if socket.status == socket.CONNECTED then print( "We are connected" ) end




#### socket.buffer_size (number)

current amount of data which can be read from buffer.

this can be used in code to see if it's possible to read the amount of data that your protocol requires.


### Methods


#### socket:addEventListener()

Events are fired when there is a change in the socket. Type of event is one of following:

`CONNECT`, `READ`, `WRITE`


{code:language=none|title=main.lua}
local Sockets = require 'dmc_library.dmc_sockets'

local socketEvent_handler = function( event )
    local etype = event.type
    local socket = event.target
    local status = event.status  -- same value as socket.status

    if etype == socket.CONNECT then
        -- process CONNECT event here
        -- using above status events

    elseif etype == socket.READ then
        -- process READ event here
        local bytes = event.bytes  -- number of bytes which can be read

    elseif etype == socket.WRITE then
        -- process WRITE event here
        -- TBD

    end

end

socket:addEventListener( socket.EVENT, socketEvent_handler )

socket:clearBuffer()

purge current buffer data

socket:close()

closes connection.

same as LuaSocket close()

socket:connect( host, port )

opens connection.

same as LuaSocket connect()

see socket:reconnect()

socket:getstats()

same as LuaSocket getstats()

socket:receive( option )

reads data from socket.

same as LuaSocket receive()

see socket:unreceive()

socket:reconnect( params )

reconnects to closed or terminated socket using the same host/port as original call.

see socket:connect()

socket:send( data )

sends data using socket

same as LuaSocket send()

socket:unreceive( data )

puts data back on socket buffer.

{anchor:atcp_socket_connection}

Async TCP Socket API (same as TCP, plus the following)

This is the API for the individual Async TCP socket. it has all of the above properties and methods, with these exceptions:

Properties

timeout ( number )

number of milliseconds for the callback to wait for data before giving up.

Default is 2000 ( 2 seconds )

Methods

socket:connect( host, port, params )

opens connection.

same as LuaSocket connect(), but takes parameter:

params.onConnect which is callback for CONNECT events.

see socket:reconnect()

socket:receive( option, callback )

callback function to call when data is received, or timeout.

socket:receiveUntilNewline( callback )

will read data until a blank newline is received.

this is a convenient method used to read HTTP headers.

returns a table (array) with lines from header, each line has had newline removed.

last item in array will be empty string ""