Websocket handles

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
ExampleClose a websocket
Opens a websocket and then immediately closes it.
Code
<nowiki>
local ws, err = http.websocket("wss://echo.websocket.org")
if ws then
  -- Probably should do something interesting with the websocket before
  ws.close()
end
    </nowiki>
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
ExampleReceiving a websocket message
Opens a websocket to an echo server, sends a message and waits for the response.
Code
<nowiki>
local ws, err = http.websocket("wss://echo.websocket.org")
if ws then
  ws.send("Hello")
  print(ws.receive())
  ws.close()
end
    </nowiki>
Output
<nowiki>Hello</nowiki>
Functionwebsocket.send
Sends a message to the server using this websocket
Syntax websocket.send(
  • message : string
  • binary? : boolean
)
Part of CC:Tweaked (source)
API http
ExampleSend a message to a server
Opens a websocket to a server and sends a message.
Code
<nowiki>
local ws, err = http.websocket("wss://echo.websocket.org")
if ws then
  ws.send("Hello")
  ws.close()
end
    </nowiki>
This article is issued from Computercraft. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.