OpenWebSocket()

Syntax

OpenWebSocket(#WebSocket, URL$)
Description
Open a new WebSocket at the specified URL$. The event #PB_Event_WebSocket needs to be bind with BindEvent() to monitor the WebSocket activity. See EventType() to see which event types are supported.

Parameters

#WebSocket A number to identify the new WebSocket. #PB_Any can be used to auto-generate this number.
URL$ The URL$ to connect to. It usually starts with 'ws://'.

Return value

Returns #True if the WebSocket feature is available on the device, or #False otherwise. It doesn't mean the WebSocket is actually connected, the #PB_Event_WebSocket needs to be monitored.

Example: Simple WebSocket connection

  Procedure Events()
    Select Event()
      Case #PB_Event_WebSocket
        
        Select EventType()
          Case #PB_EventType_Connected
            Debug "WebSocket #" + EventWebSocket() + " connected."
            
          Case #PB_EventType_Closed
            Debug "WebSocket #" + EventWebSocket() + " closed."
          
          Case #PB_EventType_Error
            Debug "Error on WebSocket #" + EventWebSocket() + "."
            
        EndSelect
    EndSelect
    
  EndProcedure

  ; Bind the event to monitor web sockets
  BindEvent(#PB_Event_WebSocket, @Events())

  ; Connect to a free online websocket which send back every command
  If OpenWebSocket(2, "wss://ws.postman-echo.com/raw") 
    Debug "Trying to open the websocket"
  Else
    Debug "Web socket not supported."
  EndIf

See Also

CloseWebSocket(), SendWebSocketData(), SendWebSocketString()

Supported OS

All

<- CloseWebSocket() - WebSocket Index - SendWebSocketData() ->