websocket_8_notes.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. #include <boost/asio/spawn.hpp>
  19. namespace {
  20. #include "websocket_common.ipp"
  21. void
  22. snippets()
  23. {
  24. stream<tcp_stream> ws(ioc);
  25. {
  26. //[code_websocket_8_1
  27. flat_buffer buffer;
  28. ws.async_read(buffer,
  29. [](error_code, std::size_t)
  30. {
  31. // Do something with the buffer
  32. });
  33. //]
  34. }
  35. multi_buffer b;
  36. {
  37. //[code_websocket_8_2
  38. ws.async_read(b, [](error_code, std::size_t){});
  39. ws.async_read(b, [](error_code, std::size_t){});
  40. //]
  41. }
  42. {
  43. //[code_websocket_8_3
  44. ws.async_read(b, [](error_code, std::size_t){});
  45. ws.async_write(b.data(), [](error_code, std::size_t){});
  46. ws.async_ping({}, [](error_code){});
  47. ws.async_close({}, [](error_code){});
  48. //]
  49. }
  50. }
  51. // workaround for https://github.com/chriskohlhoff/asio/issues/112
  52. //#ifdef BOOST_MSVC
  53. //[code_websocket_8_1f
  54. void echo(stream<tcp_stream>& ws,
  55. multi_buffer& buffer, net::yield_context yield)
  56. {
  57. ws.async_read(buffer, yield);
  58. std::future<std::size_t> fut =
  59. ws.async_write(buffer.data(), net::use_future);
  60. }
  61. //]
  62. //#endif
  63. struct websocket_8_test
  64. : public boost::beast::unit_test::suite
  65. {
  66. void
  67. run() override
  68. {
  69. BEAST_EXPECT(&snippets);
  70. BEAST_EXPECT(&echo);
  71. }
  72. };
  73. BEAST_DEFINE_TESTSUITE(beast,doc,websocket_8);
  74. } // (anon)
  75. #ifdef BOOST_MSVC
  76. #pragma warning(pop)
  77. #endif