05_control_frames.qbk 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. [/
  2. Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. Official repository: https://github.com/boostorg/beast
  6. ]
  7. [section Control Frames]
  8. Control frames are small (less than 128 bytes) messages entirely contained
  9. in an individual WebSocket frame. They may be sent at any time by either
  10. peer on an established connection, and can appear in between continuation
  11. frames for a message. There are three types of control frames: ping, pong,
  12. and close.
  13. A sent ping indicates a request that the sender wants to receive a pong. A
  14. pong is a response to a ping. Pongs may be sent unsolicited, at any time.
  15. One use for an unsolicited pong is to inform the remote peer that the
  16. session is still active after a long period of inactivity. A close frame
  17. indicates that the remote peer wishes to close the WebSocket connection.
  18. The connection is considered gracefully closed when each side has sent
  19. and received a close frame.
  20. During read operations, Beast automatically reads and processes control
  21. frames. If a control callback is registered, the callback is notified of
  22. the incoming control frame. The implementation will respond to pings
  23. automatically. The receipt of a close frame initiates the WebSocket
  24. close procedure, eventually resulting in the error code
  25. [link beast.ref.boost__beast__websocket__error `error::closed`]
  26. being delivered to the caller in a subsequent read operation, assuming
  27. no other error takes place.
  28. A consequence of this automatic behavior is that caller-initiated read
  29. operations can cause socket writes. However, these writes will not
  30. compete with caller-initiated write operations. For the purposes of
  31. correctness with respect to the stream invariants, caller-initiated
  32. read operations still only count as a read. This means that callers can
  33. have a simultaneously active read, write, and ping/pong operation in
  34. progress, while the implementation also automatically handles control
  35. frames.
  36. [heading Control Callback]
  37. Ping, pong, and close messages are control frames which may be sent at
  38. any time by either peer on an established WebSocket connection. They
  39. are sent using the functions
  40. [link beast.ref.boost__beast__websocket__stream.ping `ping`],
  41. [link beast.ref.boost__beast__websocket__stream.pong `pong`].
  42. and
  43. [link beast.ref.boost__beast__websocket__stream.close `close`].
  44. To be notified of control frames, callers may register a
  45. ['control callback] using
  46. [link beast.ref.boost__beast__websocket__stream.control_callback `control_callback`].
  47. The object provided with this option should be callable with the following
  48. signature:
  49. [code_websocket_5_1]
  50. When a control callback is registered, it will be invoked for all pings,
  51. pongs, and close frames received through either synchronous read functions
  52. or asynchronous read functions. The type of frame and payload text are
  53. passed as parameters to the control callback. If the frame is a close
  54. frame, the close reason may be obtained by calling
  55. [link beast.ref.boost__beast__websocket__stream.reason `reason`].
  56. Unlike regular completion handlers used in calls to asynchronous initiation
  57. functions, the control callback only needs to be set once. The callback is
  58. not reset after being called. The same callback is used for both synchronous
  59. and asynchronous reads. The callback is passive; in order to be called,
  60. a stream read operation must be active.
  61. [note
  62. When an asynchronous read function receives a control frame, the
  63. control callback is invoked in the same manner as that used to
  64. invoke the final completion handler of the corresponding read
  65. function.
  66. ]
  67. [heading Close Frames]
  68. The WebSocket protocol defines a procedure and control message for
  69. initiating a close of the session. In this procedure, a host requests
  70. the close by sending a
  71. [@https://tools.ietf.org/html/rfc6455#section-5.5.1 ['close frame]].
  72. To request a close use a close function such as
  73. [link beast.ref.boost__beast__websocket__stream.close.overload2 `close`] or
  74. [link beast.ref.boost__beast__websocket__stream.async_close `async_close`]:
  75. [code_websocket_5_2]
  76. The close function will send a close frame, read and discard incoming
  77. message data until receiving a close frame, and then shut down the
  78. underlying connection before returning.
  79. When a close frame is received by during a read operation, the implementation
  80. will automatically respond with a close frame and then shut down the
  81. underlying connection before returning. In this case, the read operation
  82. will complete with the code
  83. [link beast.ref.boost__beast__websocket__error `error::closed`].
  84. This indicates to the caller that the connection has been closed cleanly.
  85. [important
  86. To receive the
  87. [link beast.ref.boost__beast__websocket__error `error::closed`]
  88. error, a read operation is required.
  89. ]
  90. [heading Auto-fragment]
  91. To ensure timely delivery of control frames, large outgoing messages can
  92. be broken up into smaller sized frames. The automatic fragment option
  93. turns on this feature, and the write buffer size option determines the
  94. maximum size of the fragments:
  95. [code_websocket_5_3]
  96. [endsect]