websocket_success event

From ComputerCraft
Jump to: navigation, search
websocket_success
Event
Returns
  • url : string The URL of the websocket
  • websocket : table The websocket's handle.

Source CC:Tweaked

Fired when a websocket is opened with http.websocketAsync.

ExampleExampleAttempt to open a websocket and wait for it to connect.
Attempts to start a connection to an echo server and prints when connected.
Code
local wsUrl = "wss://echo.websocket.org"
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