extension-websocket

Namespace: websocket Language: Lua Type: Extension

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

API

websocket.connect

Type: FUNCTION Connects to a remote address

Parameters

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

Type: FUNCTION Explicitly close a websocket

Parameters

websocket.send

Type: FUNCTION Send data on a websocket

Parameters

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

EVENT_CONNECTED

Type: VARIABLE The websocket was connected

EVENT_DISCONNECTED

Type: VARIABLE The websocket disconnected

EVENT_MESSAGE

Type: VARIABLE The websocket received data

EVENT_ERROR

Type: VARIABLE The websocket encountered an error