Class WebSocketClient() Professional

Class function of the WebSocketClient class.

Description

Use the WebSocketClient class to implement the client of a websocket connection. A user-defined websocket client is derived from the class WebSocketClient and implements the methods ::onConnect(), :onDisconnect(), :onText()and :onBinary().

The example below demonstrates the implementation of the client side of a websocket connection. Refer to the example of WebSocketHandler()to see the implementation of the server side.

By default, websocket communication is unencrypted. However, the WebsocketClient class also supports establishing connections encrypted via a Secure Socket Layer (SSL). For this, 443 must be specified as the port number when creating the websocket client object via the method :new().

Class methods
:new()
Creates an instance of the WebSocketClient class
Connection
:connect()
Connects to a websocket server.
:disconnect()
Disconnect from a websocket server
:isConnected()
Test if the client is connected with the server
:handleMessage()
Process the next message sent from the server
Inbound
:onText()
Handles text messages sent from the server
:onBinary()
Handles binary messages sent from the server
:onConnect()
Connection handler
:onDisconnect()
Disconnection handler
Outbound
:sendText()
Send a text message to the server
:sendBinary()
Send a binary message to the server
Examples
Implement the client side of a websocket connection
// The example shows a websocket client that is 
// sending a text message to a connected server 
// once in a second. The echoed response is 
// printed to the console. 

#define SERVERNAME  GetEnv("Computername" ) 
#define PORT        81 

// 
// The class echo client serves as client 
// side of the EchoServer 
// 
CLASS EchoClient FROM WebSocketClient 
  EXPORTED 
    METHOD onText 
    METHOD onConnect 
    METHOD onDisconnect 
ENDCLASS 

// 
// Implement the methods used by the 
// EchoClient class 
// 
METHOD EchoClient:onText( cText ) 
  ? ProcName(), cText 
RETURN self 

METHOD EchoClient:onConnect() 
  ? ProcName() 
RETURN self 

METHOD EchoClient:onDisconnect() 
  ? ProcName() 
RETURN 

PROCEDURE Main 
  LOCAL oEchoClient, lHandled 

  // 
  // Instantiate the client side of the websocket 
  // connection and connect it to the server 
  // 
  oEchoClient := EchoClient():new( SERVERNAME,   ; 
                                   "EchoServer", ; 
                                   PORT ) 

  IF .NOT. oEchoClient:connect() 
    ? "Connection failed" 
    RETURN 
  ENDIF 

  // 
  // As long as the connection is active 
  // handle all messages sent from the server 
  // in non blocking mode. In case no message 
  // was handled sleep for a while and then 
  // send a textual message to the server 
  // 
  DO WHILE oEchoClient:isConnected() 
    lHandled := oEchoClient:handleMessage( .F. ) 
    IF .NOT. lHandled 
      Sleep( 100 ) 
      oEchoClient:sendText( "Hallo" ) 
    ENDIF 
  ENDDO 

RETURN 
Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.