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 HttpEndpoint an 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.
// 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 "web.ch"
#define PORT 81
PROCEDURE Main()
LOCAL oHttpEndpoint
oHttpEndpoint := HttpEndpoint():new( PORT, "localhost" )
oHttpEndpoint:start()
? "HttpEndpoint is running."
wait
oHttpEndpoint:stop()
RETURN
// A websocket handler must implement the methods
// :onConnect(), onDisconnect(), onText() and
// :onBinary().
//
// The methods are triggered from the HttpEndpoint.
//
CLASS EchoServer FROM WebSocketHandler
EXPORTED:
METHOD onConnect
METHOD onDisconnect
METHOD onText
METHOD onBinary
ENDCLASS
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.