04_messages.qbk 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. [/-----------------------------------------------------------------------------]
  8. [section:messages Messages]
  9. Once a websocket session is established, messages can be sent unsolicited by
  10. either peer at any time. A message is made up of one or more ['messages frames].
  11. Each frame is prefixed with the size of the payload in bytes, followed by the
  12. data. A frame also contains a flag (called 'fin') indicating whether or not it
  13. is the last frame of the message. When a message is made up from only one frame,
  14. it is possible to know immediately what the size of the message will be.
  15. Otherwise, the total size of the message can only be determined once
  16. the last frame is received.
  17. The boundaries between frames of a multi-frame message are not not considered
  18. part of the message. Intermediaries such as proxies which forward the websocket
  19. traffic are free to "reframe" (split frames and combine them) the message in
  20. arbitrary ways. These intermediaries include Beast, which can reframe messages
  21. automatically in some cases depending on the options set on the stream.
  22. [caution
  23. An algorithm should never depend on the way that incoming or outgoing
  24. messages are split up into frames.
  25. ]
  26. Messages can be either text or binary. A message sent as text must contain
  27. consist of valid utf8, while a message sent as binary may contain arbitrary
  28. data. In addition to message frames, websocket provides ['control frames]
  29. in the form of ping, pong, and close messages which have a small upper limit
  30. on their payload size. Depending on how a message is framed, control frames
  31. may have more opportunities to be sent in-between.
  32. [heading Sending]
  33. These stream members are used to write websocket messages:
  34. [table WebSocket Stream Write Operations
  35. [[Function][Description]]
  36. [
  37. [
  38. [link beast.ref.boost__beast__websocket__stream.write.overload2 `write`],
  39. [link beast.ref.boost__beast__websocket__stream.async_write `async_write`]
  40. ][
  41. Send a buffer sequence as a complete message.
  42. ]
  43. ][
  44. [
  45. [link beast.ref.boost__beast__websocket__stream.write_some.overload2 `write_some`],
  46. [link beast.ref.boost__beast__websocket__stream.async_write_some `async_write_some`]
  47. ][
  48. Send a buffer sequence as part of a message.
  49. ]
  50. ]]
  51. This example shows how to send a buffer sequence as a complete message.
  52. [code_websocket_4_1]
  53. The same message could be sent in two or more frames thusly.
  54. [heading Receiving]
  55. [table WebSocket Stream Read Operations
  56. [[Function][Description]]
  57. [
  58. [
  59. [link beast.ref.boost__beast__websocket__stream.read.overload2 `read`],
  60. [link beast.ref.boost__beast__websocket__stream.async_read `async_read`]
  61. ][
  62. Read a complete message into a __DynamicBuffer__.
  63. ]
  64. ][
  65. [
  66. [link beast.ref.boost__beast__websocket__stream.read_some.overload2 `read_some`],
  67. [link beast.ref.boost__beast__websocket__stream.async_read_some.overload1 `async_read_some`]
  68. ][
  69. Read part of a message into a __DynamicBuffer__.
  70. ]
  71. ][
  72. [
  73. [link beast.ref.boost__beast__websocket__stream.read_some.overload4 `read_some`],
  74. [link beast.ref.boost__beast__websocket__stream.async_read_some.overload2 `async_read_some`]
  75. ][
  76. Read part of a message into a __MutableBufferSequence__.
  77. ]
  78. ]]
  79. After the WebSocket handshake is accomplished, callers may send and receive
  80. messages using the message oriented interface. This interface requires that
  81. all of the buffers representing the message are known ahead of time:
  82. [code_websocket_4_2]
  83. [important
  84. [link beast.ref.boost__beast__websocket__stream `websocket::stream`]
  85. is not thread-safe. Calls to stream member functions must
  86. all be made from the same implicit or explicit strand.
  87. ]
  88. [heading Frames]
  89. Some use-cases make it impractical or impossible to buffer the entire
  90. message ahead of time:
  91. * Streaming multimedia to an endpoint.
  92. * Sending a message that does not fit in memory at once.
  93. * Providing incremental results as they become available.
  94. For these cases, the partial data oriented interface may be used. This
  95. example reads and echoes a complete message using this interface:
  96. [code_websocket_4_3]
  97. [endsect]