websocket_client_sync.cpp 3.2 KB

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