|
| | WebSocket (Client &client) |
| | Creates a WebSocket that handshakes through client.
|
|
| ~WebSocket () |
| | Calls close().
|
| void | beginConnect (const std::string &url, const std::string &origin=std::string()) |
| | Begins a client handshake for url.
|
| void | addProtocol (const std::string &name) |
| | Offers one Sec-WebSocket-Protocol name.
|
| const std::string & | protocol () const |
| | Returns the protocol name echoed by the server.
|
|
Pt::Signal< WebSocket & > & | connected () |
| | Returns the signal emitted when the handshake finishes.
|
| void | endConnect () |
| | Completes the client handshake.
|
|
WebSocketMessage & | incoming () |
| | Returns the incoming message.
|
|
WebSocketMessage & | outgoing () |
| | Returns the outgoing message.
|
| void | beginSend (bool finished=true) |
| | Begins sending outgoing().
|
| MessageProgress | endSend () |
| | Completes the send started by beginSend().
|
| void | beginReceive () |
| | Begins receiving into incoming().
|
| MessageProgress | endReceive () |
| | Completes the receive started by beginReceive().
|
| 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 | endPing () |
| | Completes the ping started by beginPing().
|
| void | beginShutdown (unsigned code=1000, const std::string &reason=std::string()) |
| | Begins a close handshake.
|
| void | endShutdown () |
| | Completes the shutdown started by beginShutdown().
|
| void | close () |
| | Releases the stream and writes no frame.
|
|
unsigned | closeCode () const |
| | Returns the close status code of the handshake that ended the stream.
|
|
const std::string & | closeReason () const |
| | Returns the close reason of the handshake that ended the stream.
|
|
Pt::Signal< WebSocket & > & | inputReady () |
| | Returns the signal emitted when data bytes were received.
|
|
Pt::Signal< WebSocket & > & | outputReady () |
| | Returns the signal emitted when data bytes were sent.
|
| Pt::Signal< WebSocket & > & | pingReady () |
| | Returns the signal emitted when a ping operation completes.
|
| Pt::Signal< WebSocket & > & | shutdownReady () |
| | Returns the signal emitted when a shutdown operation completes.
|
| Pt::Signal< WebSocket & > & | closed () |
| | Returns the signal emitted when the engine has ended the protocol.
|
| Pt::Signal< WebSocket &, const std::string & > & | pong () |
| | Returns the signal emitted for a received pong.
|
| void | setMaxMessageSize (std::size_t maxSize) |
| | Closes the stream when a data message exceeds maxSize.
|
| void | setIdleTimeout (std::size_t ms) |
| | Closes the stream after ms without an incoming frame.
|
| void | setPingInterval (std::size_t ms) |
| | Writes a keepalive ping after ms without an incoming frame.
|
| void | setPingTimeout (std::size_t ms) |
| | Closes the stream if a ping is unanswered for ms.
|
| void | setCloseTimeout (std::size_t ms) |
| | Closes the stream if a close handshake is unanswered for ms.
|
WebSocket is the client side of a WebSocket upgrade. It performs the HTTP handshake through an HTTP user agent and then sends and receives payloads on the stream that handshake produces. Host, port, event loop, timeout and TLS are settings of that agent. The handshake uses the client's timeout. The socket stores a reference and does not own it, so the agent must outlive the socket, including a handshake that is still waiting on the loop. After a finished 101 the socket formats the stream the client already owns. setIdleTimeout() bounds inbound silence, setPingInterval() a keepalive ping after inbound silence, setPingTimeout() an unanswered ping, and setCloseTimeout() a close handshake the peer never answers. The client still owns the connection. Closing the socket closes the stream. The socket does not own the stream. A client masks every frame it writes.
The example completes the handshake and then writes a text message.
{
}
{
{
return;
}
}
{
}
Pt::System::MainLoop loop;
Pt::Net::Endpoint ep("localhost", 80);
Pt::Http::Client client(loop, ep);
Pt::Http::WebSocket socket(client);
Progress of an asynchronous HTTP send or receive.
Definition Message.h:337
bool finished() const
Returns true if message is complete.
Definition Message.h:373
@ Text
UTF-8 text.
Definition WebSocketMessage.h:127
void discard()
Drops the buffered body and leaves the type.
void setType(Type type)
Sets the data type of an outgoing message.
std::iostream & body()
Returns the payload stream.
Definition WebSocketMessage.h:144
void clear()
Drops the buffered body and sets the type to Unknown.
Client handshake and messages on an upgraded stream.
Definition WebSocket.h:164
void endConnect()
Completes the client handshake.
Pt::Signal< WebSocket & > & outputReady()
Returns the signal emitted when data bytes were sent.
Definition WebSocket.h:332
void beginSend(bool finished=true)
Begins sending outgoing().
Pt::Signal< WebSocket & > & connected()
Returns the signal emitted when the handshake finishes.
Definition WebSocket.h:206
void beginReceive()
Begins receiving into incoming().
WebSocketMessage & outgoing()
Returns the outgoing message.
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
MessageProgress endSend()
Completes the send started by beginSend().
WebSocketMessage & incoming()
Returns the incoming message.
void beginConnect(const std::string &url, const std::string &origin=std::string())
Begins a client handshake for url.
ConstMethodSlot< R, ClassT, As... > slot(ClassT &object, R(BaseT::*method)(As...) const)
Returns a slot object for the given object/member pair.
Definition ConstMethod.h:172
void run()
Starts the loop.
Construct the socket with the Client that performs the handshake. Connect connected() and call beginConnect() with a request path, or with a ws:// URL whose host and port are already the client's endpoint. The path and query of that URL become the request URL; the client does not take the host from the URL. The optional second argument is the Origin header. endConnect() completes the handshake and throws if it failed. addProtocol() offers one Sec-WebSocket-Protocol name before beginConnect(). Several calls offer several names, in order. After a finished handshake protocol() is the single name the server echoed, or empty when the server selected none. An echo that was not offered fails the handshake. After that success the socket no longer uses the client for messages.
The socket owns two WebSocketMessage objects, incoming() and outgoing(), the way a client owns a request and a reply. Set the type, write the body, and call beginSend(). Call beginSend(false) while more body will be written, then beginSend(true) for the last part. outputReady() reports that data bytes were sent. endSend() returns MessageProgress. If the send step is not finished, beginSend() continues it with the same completion flag. beginReceive() fills incoming(). inputReady() reports that data bytes were received. endReceive() returns progress. After finished(), clear() drops the body so the same object can carry the next message. beginReceive() starts frame processing. Until then, incoming frames stay in the stream. Control frames are processed while a receive is outstanding. A local beginShutdown() also keeps input running until the peer replies with its close frame. The two are independent: a receive started before or during a shutdown still delivers what the peer sent before that reply.
Ping, pong, shutdown and close are socket operations, not messages.
void close()
Releases the stream and writes no frame.
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 beginShutdown(unsigned code=1000, const std::string &reason=std::string())
Begins a close handshake.
beginPing() enqueues a ping. pingReady() reports that its frame was written; call endPing() from that signal to observe a write error. A received ping is answered by the engine. After a close frame is queued nothing further is written, so beginPing() throws and a received ping is no longer answered. A received pong is reported by pong(). Neither is delivered through incoming(). beginShutdown() enqueues a close frame with a status code and a reason, then keeps reading until the peer replies. shutdownReady() reports the completed handshake; call endShutdown() from that signal to observe an error. A peer that never starts a receive never replies, so setCloseTimeout() bounds that wait. It defaults to ten seconds. Zero leaves the wait unbounded. setPingInterval() writes a keepalive ping after inbound silence. setPingTimeout() bounds that ping and an unanswered beginPing(). Interval, ping and idle default to zero on this type. A received close is answered by the engine. When no local shutdown is outstanding, closed() reports that the protocol has ended. The stream stays bound. Call close() from that signal. An outstanding shutdown completes through shutdownReady(); call endShutdown() and then close(). beginReceive() and beginShutdown() then throw. closeCode() and closeReason() report the handshake that ended the stream. No close frame leaves the code at 1006. close() releases the stream and writes no frame. It does not block and emits nothing. The destructor calls it. Destruction of the stream detaches the channel. The owner deletes this socket.
On the server the application does not construct this type. The server facade is WebSocketSession, which exposes the same message operations without a client handshake.