websocket_4_messages.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // Official repository: https://github.com/boostorg/beast
  8. //
  9. #include <boost/beast/_experimental/unit_test/suite.hpp>
  10. #ifdef BOOST_MSVC
  11. #pragma warning(push)
  12. #pragma warning(disable: 4459) // declaration hides global declaration
  13. #endif
  14. #include <boost/beast.hpp>
  15. #include <boost/beast/ssl.hpp>
  16. #include <boost/asio.hpp>
  17. #include <boost/asio/ssl.hpp>
  18. namespace {
  19. #include "websocket_common.ipp"
  20. void
  21. snippets()
  22. {
  23. stream<tcp_stream> ws(ioc);
  24. {
  25. //[code_websocket_4_1
  26. net::const_buffer b("Hello, world!", 13);
  27. // This sets all outgoing messages to be sent as text.
  28. // Text messages must contain valid utf8, this is checked
  29. // when reading but not when writing.
  30. ws.text(true);
  31. // Write the buffer as text
  32. ws.write(b);
  33. //]
  34. }
  35. {
  36. //[code_websocket_4_2
  37. // This DynamicBuffer will hold the received message
  38. flat_buffer buffer;
  39. // Read a complete message into the buffer's input area
  40. ws.read(buffer);
  41. // Set text mode if the received message was also text,
  42. // otherwise binary mode will be set.
  43. ws.text(ws.got_text());
  44. // Echo the received message back to the peer. If the received
  45. // message was in text mode, the echoed message will also be
  46. // in text mode, otherwise it will be in binary mode.
  47. ws.write(buffer.data());
  48. // Discard all of the bytes stored in the dynamic buffer,
  49. // otherwise the next call to read will append to the existing
  50. // data instead of building a fresh message.
  51. buffer.consume(buffer.size());
  52. //]
  53. }
  54. {
  55. //[code_websocket_4_3
  56. // This DynamicBuffer will hold the received message
  57. multi_buffer buffer;
  58. // Read the next message in pieces
  59. do
  60. {
  61. // Append up to 512 bytes of the message into the buffer
  62. ws.read_some(buffer, 512);
  63. }
  64. while(! ws.is_message_done());
  65. // At this point we have a complete message in the buffer, now echo it
  66. // The echoed message will be sent in binary mode if the received
  67. // message was in binary mode, otherwise we will send in text mode.
  68. ws.binary(ws.got_binary());
  69. // This buffer adaptor allows us to iterate through buffer in pieces
  70. buffers_suffix<multi_buffer::const_buffers_type> cb{buffer.data()};
  71. // Echo the received message in pieces.
  72. // This will cause the message to be broken up into multiple frames.
  73. for(;;)
  74. {
  75. if(buffer_bytes(cb) > 512)
  76. {
  77. // There are more than 512 bytes left to send, just
  78. // send the next 512 bytes. The value `false` informs
  79. // the stream that the message is not complete.
  80. ws.write_some(false, buffers_prefix(512, cb));
  81. // This efficiently discards data from the adaptor by
  82. // simply ignoring it, but does not actually affect the
  83. // underlying dynamic buffer.
  84. cb.consume(512);
  85. }
  86. else
  87. {
  88. // Only 512 bytes or less remain, so write the whole
  89. // thing and inform the stream that this piece represents
  90. // the end of the message by passing `true`.
  91. ws.write_some(true, cb);
  92. break;
  93. }
  94. }
  95. // Discard all of the bytes stored in the dynamic buffer,
  96. // otherwise the next call to read will append to the existing
  97. // data instead of building a fresh message.
  98. buffer.consume(buffer.size());
  99. //]
  100. }
  101. }
  102. struct websocket_4_test
  103. : public boost::beast::unit_test::suite
  104. {
  105. void
  106. run() override
  107. {
  108. BEAST_EXPECT(&snippets);
  109. }
  110. };
  111. BEAST_DEFINE_TESTSUITE(beast,doc,websocket_4);
  112. } // (anon)
  113. #ifdef BOOST_MSVC
  114. #pragma warning(pop)
  115. #endif