WebSocket.h
1/* Copyright (C) 2015 by Laurentiu-Gheorghe Crisan
2 Copyright (C) 2026 by Marc Boris Duerner
3 SPDX-License-Identifier: LGPL-2.1-or-later WITH mif-exception
4*/
5
6#ifndef PT_HTTP_WEBSOCKET_H
7#define PT_HTTP_WEBSOCKET_H
8
9#include <Pt/Http/Api.h>
10#include <Pt/Http/WebSocketMessage.h>
11#include <Pt/Http/Message.h>
12#include <Pt/Connectable.h>
13#include <Pt/Signal.h>
14#include <string>
15#include <vector>
16#include <cstddef>
17
18namespace Pt {
19
20namespace Http {
21
22class Client;
23class WebSocketChannel;
24
25
163class PT_HTTP_API WebSocket : public Pt::Connectable
164{
165 public:
171 explicit WebSocket(Client& client);
172
176
183 void beginConnect(const std::string& url,
184 const std::string& origin = std::string());
185
194 void addProtocol(const std::string& name);
195
201 const std::string& protocol() const
202 { return _protocol; }
203
207 { return _connected; }
208
214
218
222
240 void beginSend(bool finished = true);
241
250
262
272
281 void beginPing(const char* payload = 0, std::size_t n = 0);
282
288 void endPing();
289
299 void beginShutdown(unsigned code = 1000,
300 const std::string& reason = std::string());
301
308
315 void close();
316
319 unsigned closeCode() const;
320
323 const std::string& closeReason() const;
324
328 { return _inputReady; }
329
333 { return _outputReady; }
334
340 { return _pingReady; }
341
348 { return _shutdownReady; }
349
356 { return _closed; }
357
366
372 void setMaxMessageSize(std::size_t maxSize);
373
379 void setIdleTimeout(std::size_t ms);
380
390 void setPingInterval(std::size_t ms);
391
398 void setPingTimeout(std::size_t ms);
399
405 void setCloseTimeout(std::size_t ms);
406
407 private:
408 void parseUrl(const std::string& url, const std::string& origin);
409
410 static std::string createKey();
411
412 void finishHandshake(bool failed);
413
414 void onRequestSent(Client& client);
415
416 void onReply(Client& client);
417
418 void onInputReady();
419
420 void onOutputReady();
421
422 void onPingReady();
423
424 void onShutdownReady();
425
426 void onClosed();
427
428 void onPong(const std::string& payload);
429
430 private:
431 Client* _client;
432 WebSocketChannel* _channel;
433 std::string _path;
434 std::string _protocol;
435 std::vector<std::string> _protocols;
436 bool _error;
437 bool _connecting;
438 Pt::Signal<WebSocket&> _connected;
439 Pt::Signal<WebSocket&> _inputReady;
440 Pt::Signal<WebSocket&> _outputReady;
441 Pt::Signal<WebSocket&> _pingReady;
442 Pt::Signal<WebSocket&> _shutdownReady;
445};
446
447} // namespace Http
448
449} // namespace Pt
450
451#endif
Connection Management for Signal and Slot Objects.
Definition Connectable.h:50
HTTP user agent.
Definition Client.h:196
Progress of an asynchronous HTTP send or receive.
Definition Message.h:337
Payload of one text or binary WebSocket message.
Definition WebSocketMessage.h:110
unsigned closeCode() const
Returns the close status code of the handshake that ended the stream.
void endConnect()
Completes the client handshake.
void endPing()
Completes the ping started by beginPing().
void setPingInterval(std::size_t ms)
Writes a keepalive ping after ms without an incoming frame.
Pt::Signal< WebSocket & > & outputReady()
Returns the signal emitted when data bytes were sent.
Definition WebSocket.h:332
const std::string & closeReason() const
Returns the close reason of the handshake that ended the stream.
const std::string & protocol() const
Returns the protocol name echoed by the server.
Definition WebSocket.h:201
void beginSend(bool finished=true)
Begins sending outgoing().
void close()
Releases the stream and writes no frame.
Pt::Signal< WebSocket & > & connected()
Returns the signal emitted when the handshake finishes.
Definition WebSocket.h:206
void setMaxMessageSize(std::size_t maxSize)
Closes the stream when a data message exceeds maxSize.
void setCloseTimeout(std::size_t ms)
Closes the stream if a close handshake is unanswered for ms.
void beginPing(const char *payload=0, std::size_t n=0)
Begins writing a ping with an optional payload of at most 125 bytes.
void beginReceive()
Begins receiving into incoming().
WebSocketMessage & outgoing()
Returns the outgoing message.
void setIdleTimeout(std::size_t ms)
Closes the stream after ms without an incoming frame.
Pt::Signal< WebSocket & > & pingReady()
Returns the signal emitted when a ping operation completes.
Definition WebSocket.h:339
Pt::Signal< WebSocket & > & shutdownReady()
Returns the signal emitted when a shutdown operation completes.
Definition WebSocket.h:347
MessageProgress endReceive()
Completes the receive started by beginReceive().
Pt::Signal< WebSocket & > & inputReady()
Returns the signal emitted when data bytes were received.
Definition WebSocket.h:327
void endShutdown()
Completes the shutdown started by beginShutdown().
~WebSocket()
Calls close().
MessageProgress endSend()
Completes the send started by beginSend().
WebSocketMessage & incoming()
Returns the incoming message.
void addProtocol(const std::string &name)
Offers one Sec-WebSocket-Protocol name.
void setPingTimeout(std::size_t ms)
Closes the stream if a ping is unanswered for ms.
WebSocket(Client &client)
Creates a WebSocket that handshakes through client.
void beginConnect(const std::string &url, const std::string &origin=std::string())
Begins a client handshake for url.
Pt::Signal< WebSocket &, const std::string & > & pong()
Returns the signal emitted for a received pong.
Definition WebSocket.h:364
Pt::Signal< WebSocket & > & closed()
Returns the signal emitted when the engine has ended the protocol.
Definition WebSocket.h:355
void beginShutdown(unsigned code=1000, const std::string &reason=std::string())
Begins a close handshake.
Multicast Signal to call multiple slots.
Definition Signal.h:190
HTTP clients and servers.
Core module.
Definition Allocator.h:33