websocket_failure event

From ComputerCraft
Jump to: navigation, search
websocket_failure
Event
Returns
  • url : string The URL of the websocket
  • reason : string The reason the websocket connection could not be started.

Source CC:Tweaked

Fired when a websocket requested with http.websocketAsync cannot connect.

ExampleExampleAttempt to open a websocket and wait for it to fail.
Attempts to start a connection to an invalid location prints an error when it fails.
Code
local wsUrl = "wss://echo.websocket.org/404"
local ok, err = http.websocketAsync(wsUrl)
if ok then
  while true do
    -- Wait for a websocket_success or websocket_failure event
    -- matching the desired URL
    local event, url, res = os.pullEvent(url)
    if event == "websocket_success" and url == wsUrl then
      print("Connected")
      break
    elseif event == "websocket_failure" and url == wsUrl then
    printError("Could not connect")
    break
    end
  end
end