websocket_client_sync_ssl.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. //------------------------------------------------------------------------------
  10. //
  11. // Example: WebSocket SSL client, synchronous
  12. //
  13. //------------------------------------------------------------------------------
  14. #include "example/common/root_certificates.hpp"
  15. #include <boost/beast/core.hpp>
  16. #include <boost/beast/ssl.hpp>
  17. #include <boost/beast/websocket.hpp>
  18. #include <boost/beast/websocket/ssl.hpp>
  19. #include <boost/asio/connect.hpp>
  20. #include <boost/asio/ip/tcp.hpp>
  21. #include <boost/asio/ssl/stream.hpp>
  22. #include <cstdlib>
  23. #include <iostream>
  24. #include <string>
  25. namespace beast = boost::beast; // from <boost/beast.hpp>
  26. namespace http = beast::http; // from <boost/beast/http.hpp>
  27. namespace websocket = beast::websocket; // from <boost/beast/websocket.hpp>
  28. namespace net = boost::asio; // from <boost/asio.hpp>
  29. namespace ssl = boost::asio::ssl; // from <boost/asio/ssl.hpp>
  30. using tcp = boost::asio::ip::tcp; // from <boost/asio/ip/tcp.hpp>
  31. // Sends a WebSocket message and prints the response
  32. int main(int argc, char** argv)
  33. {
  34. try
  35. {
  36. // Check command line arguments.
  37. if(argc != 4)
  38. {
  39. std::cerr <<
  40. "Usage: websocket-client-sync-ssl <host> <port> <text>\n" <<
  41. "Example:\n" <<
  42. " websocket-client-sync-ssl echo.websocket.org 443 \"Hello, world!\"\n";
  43. return EXIT_FAILURE;
  44. }
  45. auto const host = argv[1];
  46. auto const port = argv[2];
  47. auto const text = argv[3];
  48. // The io_context is required for all I/O
  49. net::io_context ioc;
  50. // The SSL context is required, and holds certificates
  51. ssl::context ctx{ssl::context::tlsv12_client};
  52. // This holds the root certificate used for verification
  53. load_root_certificates(ctx);
  54. // These objects perform our I/O
  55. tcp::resolver resolver{ioc};
  56. websocket::stream<beast::ssl_stream<tcp::socket>> ws{ioc, ctx};
  57. // Look up the domain name
  58. auto const results = resolver.resolve(host, port);
  59. // Make the connection on the IP address we get from a lookup
  60. net::connect(ws.next_layer().next_layer(), results.begin(), results.end());
  61. // Perform the SSL handshake
  62. ws.next_layer().handshake(ssl::stream_base::client);
  63. // Set a decorator to change the User-Agent of the handshake
  64. ws.set_option(websocket::stream_base::decorator(
  65. [](websocket::request_type& req)
  66. {
  67. req.set(http::field::user_agent,
  68. std::string(BOOST_BEAST_VERSION_STRING) +
  69. " websocket-client-coro");
  70. }));
  71. // Perform the websocket handshake
  72. ws.handshake(host, "/");
  73. // Send the message
  74. ws.write(net::buffer(std::string(text)));
  75. // This buffer will hold the incoming message
  76. beast::flat_buffer buffer;
  77. // Read a message into our buffer
  78. ws.read(buffer);
  79. // Close the WebSocket connection
  80. ws.close(websocket::close_code::normal);
  81. // If we get here then the connection is closed gracefully
  82. // The make_printable() function helps print a ConstBufferSequence
  83. std::cout << beast::make_printable(buffer.data()) << std::endl;
  84. }
  85. catch(std::exception const& e)
  86. {
  87. std::cerr << "Error: " << e.what() << std::endl;
  88. return EXIT_FAILURE;
  89. }
  90. return EXIT_SUCCESS;
  91. }