Class WebSocketHandler() Professional

Class function of the WebSocketHandler class.


WebsocketHandler is an abstract class which must be used to implement the handlers for websocket endpoints. To do this, a user-defined handler class derived from the class WebSocketHandler must be implemented that overloads the abstract methods :onText(), :onBinary(), :onConnect() and :onDisconnect().

On incoming websocket connections via the class HttpEndpointan object of the user-defined handler class is instantiated to service the requests from the client. For this the methods :sendText() and :sendBinary() can be used to send messages to the connected client.

The class WebSocketHandler implements the websocket protocol according RFC 6455. If a HTML browser shall be used to establish a connection with the websocket endpoint then the browser must support the websocket protocol according this standard. The following table provides an overview about HTML browsers supporting websockets according RFC 6455:

Web browser Minimum version
Internet Explorer 10
Firefox (PC) 11
Firefox (Android) 11
Chrome (PC, Mobile) 16
Safari (Mac, iOS) 6
Opera (PC, Mobile) 12.10
Android Browser -

An Xbase++ application can utilize the class WebSocketClient()to implement the client site suitable to connect to a websocket server.

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

Deferred inbound
Handles text messages sent from the client
Handles binary messages sent from the client
Connection handler
Disconnection handler
Send a text message to the client
Send a binary message to the client
Implement the server side of a websocket connection
// The example shows a websocket server that is returning all 
// text messages back to the connected client. 
// After the HttpEndpoint is started, all incoming 
// messages to EchoServer are handled by the class 
// EchoServer. 

#include "" 

#define PORT 81 

  LOCAL oHttpEndpoint 

  oHttpEndpoint := HttpEndpoint():new( PORT, "localhost" ) 

  ? "HttpEndpoint is running." 




// A websocket handler must implement the methods 
// :onConnect(), onDisconnect(), onText() and 
// :onBinary(). 
// The methods are triggered from the HttpEndpoint. 
CLASS EchoServer FROM WebSocketHandler 
    METHOD onConnect 
    METHOD onDisconnect 
    METHOD onText 
    METHOD onBinary 

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

METHOD EchoServer:onDisconnect() 
  ? ProcName() 
RETURN self 

METHOD EchoServer:onText( cText ) 
  ? ProcName(), cText 
  ::sendText( cText ) 
RETURN self 

METHOD EchoServer:onBinary( cBinary ) 
  Unused( cBinary ) 
RETURN self 


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.