WebSocket

Initializing

variant WebSocket.new(string Address)

Creates a new WebSocket object pointing at the given address and returns it. (websocket ssl is currently not supported, and will crash upon attempt to use)

Class Methods

Connect To Server

ConnectToServer()

Connects to the server specified when making the websocket object.

Send

Send(str Message)

Sends the message to the websocket server.

Close

Close(int code, str Reason)

Closes the websocket connection with the code and reason specified.

Callback Functions

On Message

OnMessage(string Data) [writeonly]

On Connect

OnConnect() [writeonly]

On Close

OnClose(int Code, string Reason) [writeonly]

On Fail

OnFail(string Reason) [writeonly]

Example

local ws = WebSocket.new("ws://echo.websocket.org")

ws.OnMessage = function(Data)
    print("received", Data)
end

ws.OnConnect = function()
    print("connected")
    ws:Send("ProtoSmasher rocks!")
end

ws.OnClose = function(Code, Reason)
    print("closed, code:", Code, "reason:", Reason)
end

ws.OnFail = function(Reason)
    print("failed to connect, reason:", Reason)
end

ws:ConnectToServer()
wait(2)
ws:Close(1000, "normal close")

Expected Output for the above example: