WebSocketServlet Class Reference

#include <Pt/Http/WebSocketServlet.h>

Release scope of the sessions one WebSocket service creates. More...

Inherits Connectable, and NonCopyable.

Public Member Functions

 WebSocketServlet (WebSocketService &service)
 Registers this servlet with service.
 ~WebSocketServlet ()
 Releases every session this servlet still holds.
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 sessions this servlet holds.

Detailed Description

WebSocketServlet holds the sessions a WebSocket service creates and releases them while that service is still fully constructed. It is not a server. The HTTP server already owns the connection and the stream. It is not a request-mapping servlet. This type does not map a URL. A URL mapping is still required so the handshake reaches the service. The service is the factory and the endpoint policy. This type asks the factory for a session, holds the session pointer, and releases it. It does not decide the session type, the allocator, or the domain teardown.

The example registers the scope before the server starts taking upgrades. 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
Release scope of the sessions one WebSocket service creates.
Definition WebSocketServlet.h:66
WebSocketService & service()
Returns the service this servlet was constructed with.
Definition WebSocketServlet.h:81

Construct it with the WebSocketService and destroy it before that service. The constructor registers this servlet, so the next upgrade of that service is delivered here. The destructor releases every session it still holds while the derived service is still fully constructed, so the virtual release call and the allocator are still there. That order is required. The service destructor cannot release the sessions, because by then the derived object is already gone.

One service has one registered servlet. A second servlet replaces the registration. The previous servlet keeps the sessions it already accepted and releases them itself. No registration, which is the state before the first servlet and after the last destructor, declines the next upgrade. The HTTP server closes a stream that has no bound session. When a stream ends, the session close callback runs and this servlet releases that session.