websocket.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. //[code_websocket_1a
  15. #include <boost/beast.hpp>
  16. #include <boost/beast/ssl.hpp>
  17. #include <boost/asio.hpp>
  18. #include <boost/asio/ssl.hpp>
  19. //]
  20. namespace {
  21. #include "websocket_common.ipp"
  22. void
  23. snippets()
  24. {
  25. {
  26. //[code_websocket_1f
  27. // This newly constructed WebSocket stream will use the specified
  28. // I/O context and have support for the permessage-deflate extension.
  29. stream<tcp_stream> ws(ioc);
  30. //]
  31. }
  32. {
  33. //[code_websocket_2f
  34. // The `tcp_stream` will be constructed with a new
  35. // strand which uses the specified I/O context.
  36. stream<tcp_stream> ws(net::make_strand(ioc));
  37. //]
  38. }
  39. {
  40. //[code_websocket_3f
  41. // Ownership of the `tcp_stream` is transferred to the websocket stream
  42. stream<tcp_stream> ws(std::move(sock));
  43. //]
  44. }
  45. {
  46. stream<tcp_stream> ws(ioc);
  47. //[code_websocket_4f
  48. // Calls `close` on the underlying `beast::tcp_stream`
  49. ws.next_layer().close();
  50. //]
  51. }
  52. {
  53. //[code_websocket_5f
  54. // The WebSocket stream will use SSL and a new strand
  55. stream<ssl_stream<tcp_stream>> wss(net::make_strand(ioc), ctx);
  56. //]
  57. //[code_websocket_6f
  58. // Perform the SSL handshake in the client role
  59. wss.next_layer().handshake(net::ssl::stream_base::client);
  60. //]
  61. //[code_websocket_7f
  62. // Cancel all pending I/O on the underlying `tcp_stream`
  63. get_lowest_layer(wss).cancel();
  64. //]
  65. }
  66. }
  67. struct doc_websocket_test
  68. : public boost::beast::unit_test::suite
  69. {
  70. void
  71. run() override
  72. {
  73. BEAST_EXPECT(&snippets);
  74. }
  75. };
  76. BEAST_DEFINE_TESTSUITE(beast,doc,doc_websocket);
  77. } // (anon)
  78. #ifdef BOOST_MSVC
  79. #pragma warning(pop)
  80. #endif