Class WebSocketClient() Professional

Class function of the WebSocketClient class.


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
Creates an instance of the WebSocketClient class
Connects to a websocket server.
Disconnect from a websocket server
Test if the client is connected with the server
Process the next message sent from the server
Handles text messages sent from the server
Handles binary messages sent from the server
Connection handler
Disconnection handler
Send a text message to the server
Send a binary message to the server
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 
    METHOD onText 
    METHOD onConnect 
    METHOD onDisconnect 

// 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() 

  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" 

  // 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" ) 


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.