websocket_1_connecting.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. {
  24. //[code_websocket_1_1
  25. stream<tcp_stream> ws(ioc);
  26. net::ip::tcp::resolver resolver(ioc);
  27. // Connect the socket to the IP address returned from performing a name lookup
  28. get_lowest_layer(ws).connect(resolver.resolve("example.com", "ws"));
  29. //]
  30. }
  31. {
  32. //[code_websocket_1_2
  33. net::ip::tcp::acceptor acceptor(ioc);
  34. acceptor.bind(net::ip::tcp::endpoint(net::ip::tcp::v4(), 0));
  35. acceptor.listen();
  36. // The socket returned by accept() will be forwarded to the tcp_stream,
  37. // which uses it to perform a move-construction from the net::ip::tcp::socket.
  38. stream<tcp_stream> ws(acceptor.accept());
  39. //]
  40. }
  41. {
  42. net::ip::tcp::acceptor acceptor(ioc);
  43. //[code_websocket_1_3
  44. // The stream will use the strand for invoking all completion handlers
  45. stream<tcp_stream> ws(net::make_strand(ioc));
  46. // This overload of accept uses the socket provided for the new connection.
  47. // The function `tcp_stream::socket` provides access to the low-level socket
  48. // object contained in the tcp_stream.
  49. acceptor.accept(get_lowest_layer(ws).socket());
  50. //]
  51. }
  52. }
  53. struct doc_websocket_1_test
  54. : public boost::beast::unit_test::suite
  55. {
  56. void
  57. run() override
  58. {
  59. BEAST_EXPECT(&snippets);
  60. }
  61. };
  62. BEAST_DEFINE_TESTSUITE(beast,doc,doc_websocket_1);
  63. } // (anon)
  64. #ifdef BOOST_MSVC
  65. #pragma warning(pop)
  66. #endif