Websocket handles

From ComputerCraft
Jump to: navigation, search
FunctionFunctionwebsocket.close
Closes this websocket handle. You should always do this after finishing using a websocket, in order to free up resources.
Syntax websocket.close()
Part of CC:Tweaked (source)
API http
ExampleExampleClose a websocket
Opens a websocket and then immediately closes it.
Code
local ws, err = http.websocket("wss://echo.websocket.org")
if ws then
  -- Probably should do something interesting with the websocket before
  ws.close()
end


FunctionFunctionwebsocket.receive
Wait until a message has been received from this websocket.
Syntax websocket.receive()
Returns number
Part of CC:Tweaked (source)
API http
See also websocket_message
ExampleExampleReceiving a websocket message
Opens a websocket to an echo server, sends a message and waits for the response.
Code
local ws, err = http.websocket("wss://echo.websocket.org")
if ws then
  ws.send("Hello")
  print(ws.receive())
  ws.close()
end
Output
Hello


FunctionFunctionwebsocket.send
Sends a message to the server using this websocket
Syntax websocket.send()
Part of CC:Tweaked (source)
API http
ExampleExampleSend a message to a server
Opens a websocket to a server and sends a message.
Code
local ws, err = http.websocket("wss://echo.websocket.org")
if ws then
  ws.send("Hello")
  ws.close()
end