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 <cstddef>
16
17namespace Pt {
18
19namespace Http {
20
21class Client;
22class WebSocketConnection;
23
24
132class PT_HTTP_API WebSocket : public Pt::Connectable
133{
134 public:
140 explicit WebSocket(Client& client);
141
145
152 void beginConnect(const std::string& url,
153 const std::string& origin = std::string());
154
158 { return _connected; }
159
165
169
173
182 void beginSend();
183
189
196
202
208 void ping(const char* payload = 0, std::size_t n = 0);
209
218 void close(unsigned code = 1000,
219 const std::string& reason = std::string());
220
223 unsigned closeCode() const;
224
227 const std::string& closeReason() const;
228
232 { return _inputReady; }
233
237 { return _outputReady; }
238
248 { return _closed; }
249
254 void setTimeout(std::size_t timeout);
255
261 void setMaxMessageSize(std::size_t maxSize);
262
268 void setIdleTimeout(std::size_t ms);
269
270 private:
271 void parseUrl(const std::string& url, const std::string& origin);
272
273 static std::string createKey();
274
275 void finishHandshake(bool failed);
276
277 void onRequestSent(Client& client);
278
279 void onReply(Client& client);
280
281 void onInputReady();
282
283 void onOutputReady();
284
285 void onClosed();
286
287 private:
288 Client* _client;
289 WebSocketConnection* _connection;
290 std::string _path;
291 bool _error;
292 bool _connecting;
293 Pt::Signal<WebSocket&> _connected;
294 Pt::Signal<WebSocket&> _inputReady;
295 Pt::Signal<WebSocket&> _outputReady;
297};
298
299} // namespace Http
300
301} // namespace Pt
302
303#endif
Connection Management for Signal and Slot Objects.
Definition Connectable.h:50
HTTP user agent.
Definition Client.h:195
Progress of an asynchronous HTTP send or receive.
Definition Message.h:337
Payload of one text or binary WebSocket message.
Definition WebSocketMessage.h:71
void beginSend()
Begins sending outgoing().
unsigned closeCode() const
Returns the close status code of the handshake that ended the stream.
void ping(const char *payload=0, std::size_t n=0)
Enqueues a ping with an optional payload of at most 125 bytes.
void endConnect()
Completes the client handshake.
Pt::Signal< WebSocket & > & outputReady()
Returns the signal emitted when data bytes were sent.
Definition WebSocket.h:236
const std::string & closeReason() const
Returns the close reason of the handshake that ended the stream.
void setTimeout(std::size_t timeout)
Sets the stream timeout in milliseconds.
void close(unsigned code=1000, const std::string &reason=std::string())
Enqueues a close frame and ends the stream after the close handshake.
Pt::Signal< WebSocket & > & connected()
Returns the signal emitted when the handshake finishes.
Definition WebSocket.h:157
void setMaxMessageSize(std::size_t maxSize)
Closes the stream when a data message exceeds maxSize.
void beginReceive()
Begins receiving into incoming().
WebSocketMessage & outgoing()
Returns the outgoing message.
void setIdleTimeout(std::size_t ms)
Closes the stream after ms without a finished transfer.
MessageProgress endReceive()
Completes the receive started by beginReceive().
Pt::Signal< WebSocket & > & inputReady()
Returns the signal emitted when data bytes were received.
Definition WebSocket.h:231
~WebSocket()
Destructor.
MessageProgress endSend()
Completes the send started by beginSend().
WebSocketMessage & incoming()
Returns the incoming message.
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 & > & closed()
Returns the signal emitted when the stream ends.
Definition WebSocket.h:247
Multicast Signal to call multiple slots.
Definition Signal.h:190
HTTP clients and servers.
Core module.
Definition Allocator.h:33