websocket_message event

From ComputerCraft
Jump to: navigation, search
websocket_message
Event
Returns
  • url : string The URL of the websocket
  • contents : string The contents of the message

Source CC:Tweaked

Fired when a websocket message is received. websocket.receive acts as a wrapper around this event, allowing you to write code in a a more linear fashion.

ExampleExampleWait for a websocket_message event
Open a websocket, send a message and then wait for an incoming message.
Code
local wsUrl = "wss://echo.websocket.org/"
local ws, err = http.websocket(wsUrl)
if ws then
  ws.send("Hello")
  while true do
    local event, url, contents = os.pullEvent("websocket_message")
    if url == wsUrl then
      print(contents)
      break
    end
  end
  ws.close()
end