Skip to main content

Create

Creates a WebSocket Instance associated with the WebSocket object. This is not static!

Method Parameters

Parameter NameParameter TypeParameter Description
urlstringThe WebSocket URL to connect to
OnOpenDelegate?Function to be invoked when the Socket opens a connection (Optional)
OnMessageDelegate?Function to be invoked when a message is received from the server (Optional)
OnCloseDelegate?Function to be invoked when the Socket closes a connection (Optional)
OnErrorDelegate?Function to be invoked when the Socket closes a connection because of an error (Optional)

The Following example opens a WebSocket, sends an open message (see Send), and handles new messages.

JavaScript

let ws = new WebSocket()
ws.Create("ws://example.com/", () => ws.Send("I'm Here!"), data => {
if(data === "ping")
ws.Send("pong")
print("Got message! Message: " + data)
})
ws.Open()
// ...

Lua

local ws = WebSocket()
ws.Create("ws://example.com/", function() ws.Send("I'm Here!") end, function(data)
-- in case data is not a string
local s = tostring(data)
if s == "ping" then ws.Send("pong") end
print("Got message! Message: "..s)
end)
ws.Open()
-- ...