Version: stable
FUNCTION | |
---|---|
connect() | |
disconnect() | |
send() |
CONSTANT | |
---|---|
EVENT_CONNECTED | |
EVENT_DISCONNECTED | |
EVENT_MESSAGE | |
EVENT_ERROR |
connect(url,params,callback)
PARAMETERS
url |
url of the remote connection |
params |
optional parameters as properties. The following parameters can be set |
callback |
callback that receives all messages from the connection |
RETURNS
object |
the connection |
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.org" 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
disconnect(connection)
PARAMETERS
connection |
the websocket connection |
send(connection,message,options)
PARAMETERS
connection |
the websocket connection |
message |
the message to send |
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
The websocket was connected
The websocket disconnected
The websocket received data
The websocket encountered an error