Files
boost_beast/doc/qbk/06_websocket/6_control.qbk

116 lines
5.0 KiB
Plaintext
Raw Normal View History

2017-06-03 18:40:28 -07:00
[/
2017-07-28 19:32:33 -07:00
Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
2017-06-03 18:40:28 -07:00
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
2017-07-28 19:32:33 -07:00
Official repository: https://github.com/boostorg/beast
2017-06-03 18:40:28 -07:00
]
2017-06-16 19:27:00 -07:00
[section Control Frames]
2017-06-03 18:40:28 -07:00
Control frames are small (less than 128 bytes) messages entirely contained
in an individual WebSocket frame. They may be sent at any time by either
peer on an established connection, and can appear in between continuation
frames for a message. There are three types of control frames: ping, pong,
and close.
A sent ping indicates a request that the sender wants to receive a pong. A
pong is a response to a ping. Pongs may be sent unsolicited, at any time.
One use for an unsolicited pong is to inform the remote peer that the
session is still active after a long period of inactivity. A close frame
indicates that the remote peer wishes to close the WebSocket connection.
The connection is considered gracefully closed when each side has sent
and received a close frame.
During read operations, Beast automatically reads and processes control
frames. If a control callback is registered, the callback is notified of
the incoming control frame. The implementation will respond to pings
automatically. The receipt of a close frame initiates the WebSocket
close procedure, eventually resulting in the error code
2017-07-23 22:49:57 -07:00
[link beast.ref.boost__beast__websocket__error `error::closed`]
being delivered to the caller in a subsequent read operation, assuming
no other error takes place.
2017-06-03 18:40:28 -07:00
A consequence of this automatic behavior is that caller-initiated read
operations can cause socket writes. However, these writes will not
compete with caller-initiated write operations. For the purposes of
correctness with respect to the stream invariants, caller-initiated
read operations still only count as a read. This means that callers can
have a simultaneously active read, write, and ping/pong operation in
progress, while the implementation also automatically handles control
frames.
2017-06-03 18:40:28 -07:00
[heading Control Callback]
2017-06-03 18:40:28 -07:00
Ping, pong, and close messages are control frames which may be sent at
any time by either peer on an established WebSocket connection. They
are sent using the functions
2017-07-23 22:49:57 -07:00
[link beast.ref.boost__beast__websocket__stream.ping `ping`],
[link beast.ref.boost__beast__websocket__stream.pong `pong`].
and
2017-07-23 22:49:57 -07:00
[link beast.ref.boost__beast__websocket__stream.close `close`].
To be notified of control frames, callers may register a
['control callback] using
2017-07-23 22:49:57 -07:00
[link beast.ref.boost__beast__websocket__stream.control_callback `control_callback`].
2017-06-03 18:40:28 -07:00
The object provided with this option should be callable with the following
signature:
2017-06-08 15:23:30 -07:00
[ws_snippet_17]
2017-06-03 18:40:28 -07:00
When a control callback is registered, it will be invoked for all pings,
pongs, and close frames received through either synchronous read functions
or asynchronous read functions. The type of frame and payload text are
passed as parameters to the control callback. If the frame is a close
frame, the close reason may be obtained by calling
2017-07-23 22:49:57 -07:00
[link beast.ref.boost__beast__websocket__stream.reason `reason`].
2017-06-03 18:40:28 -07:00
Unlike regular completion handlers used in calls to asynchronous initiation
functions, the control callback only needs to be set once. The callback is
not reset after being called. The same callback is used for both synchronous
and asynchronous reads. The callback is passive; in order to be called,
a stream read operation must be active.
2017-06-03 18:40:28 -07:00
[note
When an asynchronous read function receives a control frame, the
control callback is invoked in the same manner as that used to
invoke the final completion handler of the corresponding read
function.
2017-06-03 18:40:28 -07:00
]
[heading Close Frames]
The WebSocket protocol defines a procedure and control message for initiating
a close of the session. Handling of close initiated by the remote end of the
connection is performed automatically. To manually initiate a close, use
the
2017-07-23 22:49:57 -07:00
[link beast.ref.boost__beast__websocket__stream.close `close`] function:
2017-06-08 15:23:30 -07:00
[ws_snippet_18]
2017-06-03 18:40:28 -07:00
When the remote peer initiates a close by sending a close frame, Beast
will handle it for you by causing the next read to return `error::closed`.
When this error code is delivered, it indicates to the application that
the WebSocket connection has been closed cleanly, and that the TCP/IP
connection has been closed. After initiating a close, it is necessary to
continue reading messages until receiving the error `error::closed`. This
is because the remote peer may still be sending message and control frames
before it receives and responds to the close frame.
[important
To receive the
2017-07-23 22:49:57 -07:00
[link beast.ref.boost__beast__websocket__error `error::closed`]
2017-06-03 18:40:28 -07:00
error, a read operation is required.
]
[heading Auto-fragment]
To ensure timely delivery of control frames, large messages can be broken up
into smaller sized frames. The automatic fragment option turns on this
feature, and the write buffer size option determines the maximum size of
the fragments:
2017-06-08 15:23:30 -07:00
[ws_snippet_19]
2017-06-03 18:40:28 -07:00
[endsect]