Websocket handles

From ComputerCraft
Jump to navigation Jump to search
Error creating thumbnail: Unable to save thumbnail to destination
Functionwebsocket.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
Error creating thumbnail: Unable to save thumbnail to destination
ExampleClose 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


Error creating thumbnail: Unable to save thumbnail to destination
Functionwebsocket.receive
Wait until a message has been received from this websocket.
Syntax websocket.receive()
Returns string
Part of CC:Tweaked (source)
API http
See also websocket_message
Error creating thumbnail: Unable to save thumbnail to destination
ExampleReceiving 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


Error creating thumbnail: Unable to save thumbnail to destination
Functionwebsocket.send
Sends a message to the server using this websocket
Syntax websocket.send()
Part of CC:Tweaked (source)
API http
Error creating thumbnail: Unable to save thumbnail to destination
ExampleSend 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