-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWebSocketStream
More file actions
98 lines (77 loc) · 2.52 KB
/
WebSocketStream
File metadata and controls
98 lines (77 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/*
* Copyright (C) 2025 Frank Mertens.
*
* Distribution and use is allowed under the terms of the GNU General Public License version 3
* (see CoreComponents/LICENSE-gpl-3.0).
*
*/
#pragma once
#include <cc/WebSocketMessage>
#include <cc/WebSocketStatus>
#include <cc/Stream>
#include <cc/Function>
namespace cc {
/** \class WebSocketStream cc/WebSocketStream
* \ingroup http_protocol
* \brief WebSocket message stream
* \todo add a waitUntil();
* \todo add configuration option and logic for websocket keep alive interval (two misses result in connection termination)
* \todo make sure WebSocket connections are brought down cleanly on server shutdown
*/
class WebSocketStream final: public Object
{
public:
/** Communication direction
*/
enum Type {
ServerToClient = 0, ///< This WebSocketStream is used by the server
ClientToServer = 1 ///< This WebSocketStream is used by the client
};
/** Create a null object
*/
WebSocketStream() = default;
/** Create a new frame stream
*/
explicit WebSocketStream(const Stream &stream, Type type);
/** %Set the maximum frame payload size
*/
WebSocketStream &maxOutgoingFramePayloadSize(long size);
/** %Set a limit for the maximum incoming message size
*/
WebSocketStream &maxIncomingMessageSize(long size);
/** %Set a callback handler \a f to be invoked whenever a ping frame is received
*/
WebSocketStream &onPingReceived(Function<void()> &&f);
/** %Set a callback handler \a f to be invoked whenever a pong frame is received
*/
WebSocketStream &onPongReceived(Function<void()> &&f);
/** Receive a message
*/
bool read(Out<String> message, Out<WebSocketMessage::Type> type = None{});
/** Send a message
*/
bool write(const String &message, WebSocketMessage::Type type = WebSocketMessage::Type::Text, uint32_t mask = 0);
/** Send a message
*/
bool write(const Bytes &message, WebSocketMessage::Type type = WebSocketMessage::Type::Binary, uint32_t mask = 0);
/** Tell if this %WebSocket connection is still open
*/
bool isOpen() const;
/** Close this %WebSocket connection
*/
bool close(WebSocketStatus status);
/** %Status of this %WebSocket connection
*/
WebSocketStatus status() const;
/** Send a ping frame
*/
bool ping();
/** Send an unsolicited pong frame
*/
bool pong();
private:
struct State;
const State &me() const;
State &me();
};
} // namespace cc