ASL
WebSocketServer Class Reference

Detailed Description

This class can be used to create WebSocket servers.

To create a server, define a subclass and implement the serve() method. This function is called in a new thread when an incoming WebSocket connection arrives.

class AWebSocketServer : public WebSocketServer
{
public:
void serve(WebSocket& ws)
{
while (1) {
ws.wait();
if (ws.closed())
break;
String msg = ws.receive();
ws.send("Hello " + msg);
}
}
};
AWebSocketServer wsserver;
wsserver.bind(9000);
wsserver.start();
virtual void serve(WebSocket &s)
Serves the incoming client websocket, implement this function in a subclass to define the behavior of...

Additionally, a WebSocket server can use the same port as an HttpServer. To do this, call the link() function in the HTTP server and only start that one.

httpserver.link(wsserver);
httpserver.start();

#include <WebSocket.h>

Inheritance diagram for WebSocketServer:
SocketServer

Public Member Functions

virtual void serve (WebSocket &s)
 Serves the incoming client websocket, implement this function in a subclass to define the behavior of this server.
 
const Array< WebSocket * > & clients () const
 Returns an array of currently connected client websockets.
 
- Public Member Functions inherited from SocketServer
void start (bool nonblocking=false)
 Makes the server start listening. More...
 
bool bindPath (const String &sname)
 Assigns a Unix local socket to listen to (where supported).
 
bool bind (int port)
 Assigns a TCP port to listen to.
 
bool bind (const String &ip, int port)
 Assigns an interface IP address and TCP port to listen to.
 
bool bindTLS (const String &ip, int port)
 Assigns an interface IP address and TCP port to listen for TLS connections.
 
bool bindTLS (int port)
 Assigns a TCP port for TLS connections.
 
bool useCert (const String &cert, const String &key)
 Sets the certificate and private key for the TLS server in PEM format.
 
void stop (bool sync=false)
 Requests the server to stop receiving connections, and waits for all clients to exit if sync is true.
 
void setSequential (bool on)
 Sets the mode of operation: sequential (connections will be handled in sequence) or concurrent ( connections will be handled in parallel by starting a new thread each time); this must be called before calling start() to start the server.
 
bool running () const
 Returns true if this server started and has not yet stopped or still has clients running.
 
String socketError () const
 Returns a string representation of the last socket error (after a failed bind or use cert...)
 

The documentation for this class was generated from the following file: