WPILibC++
2019.1.1-beta-4-27-ga2368a6
|
RFC 6455 compliant WebSocket client and server implementation. More...
#include <WebSocket.h>
Classes | |
struct | ClientOptions |
Client connection options. More... | |
Public Types | |
enum | State { CONNECTING = 0, OPEN, CLOSING, FAILED, CLOSED } |
Connection states. More... | |
Public Member Functions | |
WebSocket (uv::Stream &stream, bool server, const private_init &) | |
WebSocket (const WebSocket &)=delete | |
WebSocket (WebSocket &&)=delete | |
WebSocket & | operator= (const WebSocket &)=delete |
WebSocket & | operator= (WebSocket &&)=delete |
State | GetState () const |
Get connection state. | |
bool | IsOpen () const |
Return if the connection is open. More... | |
uv::Stream & | GetStream () const |
Get the underlying stream. | |
StringRef | GetProtocol () const |
Get the selected sub-protocol. More... | |
void | SetMaxMessageSize (size_t size) |
Set the maximum message size. More... | |
void | SetCombineFragments (bool combine) |
Set whether or not fragmented frames should be combined. More... | |
void | Close (uint16_t code=1005, const Twine &reason=Twine{}) |
Initiate a closing handshake. More... | |
void | SendText (ArrayRef< uv::Buffer > data, std::function< void(MutableArrayRef< uv::Buffer >, uv::Error)> callback) |
Send a text message. More... | |
void | SendBinary (ArrayRef< uv::Buffer > data, std::function< void(MutableArrayRef< uv::Buffer >, uv::Error)> callback) |
Send a binary message. More... | |
void | SendTextFragment (ArrayRef< uv::Buffer > data, std::function< void(MutableArrayRef< uv::Buffer >, uv::Error)> callback) |
Send a text message fragment. More... | |
void | SendBinaryFragment (ArrayRef< uv::Buffer > data, std::function< void(MutableArrayRef< uv::Buffer >, uv::Error)> callback) |
Send a text message fragment. More... | |
void | SendFragment (ArrayRef< uv::Buffer > data, bool fin, std::function< void(MutableArrayRef< uv::Buffer >, uv::Error)> callback) |
Send a continuation frame. More... | |
void | SendPing (std::function< void(uv::Error)> callback=nullptr) |
Send a ping frame with no data. More... | |
void | SendPing (ArrayRef< uv::Buffer > data, std::function< void(MutableArrayRef< uv::Buffer >, uv::Error)> callback) |
Send a ping frame. More... | |
void | SendPong (std::function< void(uv::Error)> callback=nullptr) |
Send a pong frame with no data. More... | |
void | SendPong (ArrayRef< uv::Buffer > data, std::function< void(MutableArrayRef< uv::Buffer >, uv::Error)> callback) |
Send a pong frame. More... | |
void | Fail (uint16_t code=1002, const Twine &reason="protocol error") |
Fail the connection. | |
void | Terminate (uint16_t code=1006, const Twine &reason="terminated") |
Forcibly close the connection. | |
template<typename T = void> | |
std::shared_ptr< T > | GetData () const |
Gets user-defined data. More... | |
void | SetData (std::shared_ptr< void > data) |
Sets user-defined data. More... | |
Static Public Member Functions | |
static std::shared_ptr< WebSocket > | CreateClient (uv::Stream &stream, const Twine &uri, const Twine &host, ArrayRef< StringRef > protocols=ArrayRef< StringRef >{}, const ClientOptions &options=ClientOptions{}) |
Starts a client connection by performing the initial client handshake. More... | |
static std::shared_ptr< WebSocket > | CreateServer (uv::Stream &stream, StringRef key, StringRef version, StringRef protocol=StringRef{}) |
Starts a server connection by performing the initial server side handshake. More... | |
Public Attributes | |
sig::Signal< StringRef > | open |
Open event. More... | |
sig::Signal< uint16_t, StringRef > | closed |
Close event. More... | |
sig::Signal< StringRef, bool > | text |
Text message event. More... | |
sig::Signal< ArrayRef< uint8_t > , bool > | binary |
Binary message event. More... | |
sig::Signal< ArrayRef< uint8_t > > | ping |
Ping event. More... | |
sig::Signal< ArrayRef< uint8_t > > | pong |
Pong event. More... | |
RFC 6455 compliant WebSocket client and server implementation.
Initiate a closing handshake.
code | A numeric status code (defaults to 1005, no status code) |
reason | A human-readable string explaining why the connection is closing (optional). |
|
static |
Starts a client connection by performing the initial client handshake.
An open event is emitted when the handshake completes. This sets the stream user data to the websocket.
stream | Connection stream |
uri | The Request-URI to send |
host | The host or host:port to send |
protocols | The list of subprotocols |
options | Handshake options |
|
static |
Starts a server connection by performing the initial server side handshake.
This should be called after the HTTP headers have been received. An open event is emitted when the handshake completes. This sets the stream user data to the websocket.
stream | Connection stream |
key | The value of the Sec-WebSocket-Key header field in the client request |
version | The value of the Sec-WebSocket-Version header field in the client request |
protocol | The subprotocol to send to the client (in the Sec-WebSocket-Protocol header field). |
|
inline |
Gets user-defined data.
|
inline |
Get the selected sub-protocol.
Only valid in or after the open() event.
|
inline |
Return if the connection is open.
Messages can only be sent on open connections.
|
inline |
Send a binary message.
data | Data to send |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a text message fragment.
This must be followed by one or more SendFragment() calls, where the last one has fin=True, to complete the message.
data | Data to send |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a continuation frame.
This is used to send additional parts of a message started with SendTextFragment() or SendBinaryFragment().
data | Data to send |
fin | Set to true if this is the final fragment of the message |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a ping frame with no data.
callback | Optional callback which is invoked when the ping frame write completes. |
|
inline |
Send a ping frame.
data | Data to send in the ping frame |
callback | Callback which is invoked when the ping frame write completes. |
|
inline |
Send a pong frame with no data.
callback | Optional callback which is invoked when the pong frame write completes. |
|
inline |
Send a pong frame.
data | Data to send in the pong frame |
callback | Callback which is invoked when the pong frame write completes. |
|
inline |
Send a text message.
data | UTF-8 encoded data to send |
callback | Callback which is invoked when the write completes. |
|
inline |
Send a text message fragment.
This must be followed by one or more SendFragment() calls, where the last one has fin=True, to complete the message.
data | UTF-8 encoded data to send |
callback | Callback which is invoked when the write completes. |
|
inline |
Set whether or not fragmented frames should be combined.
Default is to combine. If fragmented frames are combined, the text and binary callbacks will always have the second parameter (fin) set to true.
combine | True if fragmented frames should be combined. |
|
inline |
Sets user-defined data.
data | User-defined arbitrary data. |
|
inline |
Set the maximum message size.
Default is 128 KB. If configured to combine fragments this maximum applies to the entire message (all combined fragments).
size | Maximum message size in bytes |
sig::Signal<ArrayRef<uint8_t>, bool> wpi::WebSocket::binary |
Binary message event.
Emitted when a binary message is received. The first parameter is the data, the second parameter is true if the data is the last fragment of the message.
sig::Signal<uint16_t, StringRef> wpi::WebSocket::closed |
Close event.
Emitted when the connection is closed. The first parameter is a numeric value indicating the status code explaining why the connection has been closed. The second parameter is a human-readable string explaining the reason why the connection has been closed.
sig::Signal<StringRef> wpi::WebSocket::open |
Open event.
Emitted when the connection is open and ready to communicate. The parameter is the selected subprotocol.
sig::Signal<ArrayRef<uint8_t> > wpi::WebSocket::ping |
Ping event.
Emitted when a ping message is received.
sig::Signal<ArrayRef<uint8_t> > wpi::WebSocket::pong |
Pong event.
Emitted when a pong message is received.
sig::Signal<StringRef, bool> wpi::WebSocket::text |
Text message event.
Emitted when a text message is received. The first parameter is the data, the second parameter is true if the data is the last fragment of the message.