WebSocketServlet Class Reference

#include <Pt/Http/WebSocketServlet.h>

Owner of the sessions one WebSocket service creates. More...

Inherits NonCopyable.

Public Member Functions

 WebSocketServlet (WebSocketService &service)
 Attaches this servlet to service.
 ~WebSocketServlet ()
 Detaches this servlet and releases its remaining sessions.
WebSocketService & service ()
 Returns the service this servlet was constructed with.
const WebSocketService & service () const
 Returns the service this servlet was constructed with.
std::size_t size () const
 Returns the number of live sessions.

Detailed Description

WebSocketServlet owns the live sessions of one WebSocketService. The service owns the endpoint policy and the allocator, and stays the mapped HTTP service and the factory. Destroy this servlet before the service, so each remaining session is released while the derived service, its virtual release call, and its allocator are still available. It is not a server and does not map a URL. A URL mapping is still required so the handshake reaches the service.

The example registers the servlet before the server starts taking upgrades. The three setters are endpoint policy of the service. They apply before this servlet is constructed. Destroy the servlet before the service.

EchoService service;
Pt::Http::MapUrl mapUrl("/ws", service);
server.addServlet(mapUrl);
Maps requests to a service by URL.
Definition Servlet.h:147
void setMaxSockets(std::size_t n)
Sets the maximum number of live sessions.
Definition WebSocketService.h:182
void setMaxMessageSize(std::size_t n)
Sets the maximum data message size in bytes.
Definition WebSocketService.h:278
void setIdleTimeout(std::size_t ms)
Sets the idle timeout in milliseconds.
Definition WebSocketService.h:202
Owner of the sessions one WebSocket service creates.
Definition WebSocketServlet.h:72
WebSocketService & service()
Returns the service this servlet was constructed with.
Definition WebSocketServlet.h:89

Construct it with the WebSocketService and destroy it before that service. The constructor attaches this servlet, so the handshake can accept upgrades. The destructor detaches it and releases every remaining session through releaseSession(). That order is required. The service destructor cannot release sessions, because by then the derived object is already gone.

One service has one active servlet. Constructing a second WebSocketServlet for the same service throws std::logic_error. With no servlet, the handshake answers the upgrade with 503 and opens no stream. The service owns setMaxSockets(), setIdleTimeout(), setPingInterval(), setPingTimeout(), setCloseTimeout() and setMaxMessageSize(). The handshake reads the session limit and socketCount() from the service. socketCount() is the number of sessions getSession() returned and releaseSession() has not yet released. The session base copies the idle, ping-interval, ping and close timeouts and the data-message limit from the service onto the connection before the derived constructor runs.

Constructor & Destructor Documentation

◆ WebSocketServlet()

WebSocketServlet ( WebSocketService & service)
explicit
Exceptions
%std::logic_errorif service already has an active servlet.