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... | |
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.
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.