Extension-websocket

Functions and constants for using websockets. Supported on all platforms.

Namespace: websocket
Include: #include <>
CONSTANTS
EVENT_CONNECTED
EVENT_DISCONNECTED
EVENT_MESSAGE
EVENT_ERROR
FUNCTIONS
void websocket.connect(string url, table params, function callback)
void websocket.disconnect(object connection)
void websocket.send(object connection, string message, table options)

Functions

websocket.connect

void websocket.connect(string url, table params, function callback)

Connects to a remote address

PARAMETERS

string url url of the remote connection
table params optional parameters as properties. The following parameters can be set
function callback callback that receives all messages from the connection

RETURNS

EXAMPLES

  local function websocket_callback(self, conn, data)
    if data.event == websocket.EVENT_DISCONNECTED then
      log("Disconnected: " .. tostring(conn))
      self.connection = nil
      update_gui(self)
    elseif data.event == websocket.EVENT_CONNECTED then
      update_gui(self)
      log("Connected: " .. tostring(conn))
    elseif data.event == websocket.EVENT_ERROR then
      log("Error: '" .. data.message .. "'")
    elseif data.event == websocket.EVENT_MESSAGE then
      log("Receiving: '" .. tostring(data.message) .. "'")
    end
  end

  function init(self)
    self.url = "ws://echo.websocket.events"
    local params = {
      timeout = 3000,
      headers = "Sec-WebSocket-Protocol: chat\r\nOrigin: mydomain.com\r\n"
    }
    self.connection = websocket.connect(self.url, params, websocket_callback)
  end

  function finalize(self)
      if self.connection ~= nil then
        websocket.disconnect(self.connection)
      end
  end

websocket.disconnect

void websocket.disconnect(object connection)

Explicitly close a websocket

PARAMETERS

object connection the websocket connection

websocket.send

void websocket.send(object connection, string message, table options)

Send data on a websocket

PARAMETERS

object connection the websocket connection
string message the message to send
table options options for this particular message. May be `nil`

EXAMPLES

  local function websocket_callback(self, conn, data)
    if data.event == websocket.EVENT_CONNECTED then
      websocket.send(conn, "Hello from the other side")
    end
  end

  function init(self)
    self.url = "ws://echo.websocket.org"
    local params = {}
    self.connection = websocket.connect(self.url, params, websocket_callback)
  end

Constants

EVENT_CONNECTED

The websocket was connected


EVENT_DISCONNECTED

The websocket disconnected


EVENT_MESSAGE

The websocket received data


EVENT_ERROR

The websocket encountered an error