Handshake through an HTTP client and send messages on the upgraded stream. More...

Classes

class  WebSocket
 Client handshake and messages on an upgraded stream. More...

Detailed Description

A client WebSocket is the handshake plus the message surface on the stream that handshake produces. Host, port, event loop, timeout and TLS stay settings of the HTTP user agent. The socket stores a reference to that agent 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. The client still owns the connection. Closing the socket closes the stream. The socket does not own the stream.

The example completes the handshake and then writes a text message.

void onConnected(Pt::Http::WebSocket& socket)
{
socket.endConnect();
socket.outgoing().body() << "hello";
socket.beginSend();
socket.beginReceive();
}
void onClosed(Pt::Http::WebSocket& /*socket*/)
{
}
Pt::Net::Endpoint ep("localhost", 80);
Pt::Http::Client client(loop, ep);
Pt::Http::WebSocket socket(client);
socket.connected() += Pt::slot(onConnected);
socket.closed() += Pt::slot(onClosed);
socket.setTimeout(10000);
socket.beginConnect("/ws");
loop.run();
HTTP user agent.
Definition Client.h:195
@ Text
UTF-8 text.
Definition WebSocketMessage.h:80
void setType(Type type)
Sets the data type of an outgoing message.
std::iostream & body()
Returns the payload stream.
Definition WebSocketMessage.h:97
Client handshake and messages on an upgraded stream.
Definition WebSocket.h:133
void beginSend()
Begins sending outgoing().
void endConnect()
Completes the client handshake.
void setTimeout(std::size_t timeout)
Sets the stream timeout in milliseconds.
Pt::Signal< WebSocket & > & connected()
Returns the signal emitted when the handshake finishes.
Definition WebSocket.h:157
void beginReceive()
Begins receiving into incoming().
WebSocketMessage & outgoing()
Returns the outgoing message.
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
Host and service address.
Definition Endpoint.h:77
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.
Platform event loop.
Definition MainLoop.h:51

Construct WebSocket 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. beginConnect() fills a GET with Connection, Upgrade, Sec-WebSocket-Version 13 and Sec-WebSocket-Key, and sends it through the client. A finished 101 becomes the stream this socket formats. endConnect() completes the handshake and throws if it failed. After that success the socket no longer uses the client for messages. A client masks every frame it writes.

closed() is emitted while this socket is still alive. The stream has already cleared its session pointer. Peer close, an I/O error, a close frame and destruction of the stream all emit it. It ends an outstanding send or receive. The owner deletes this socket. setTimeout() bounds I/O after the upgrade. The handshake still uses the timeout of the client.