| 119 | //------------------------------------------------------------------------------ |
| 120 | |
| 121 | int main(int argc, char** argv) |
| 122 | { |
| 123 | // Check command line arguments. |
| 124 | if(argc != 4) |
| 125 | { |
| 126 | std::cerr << |
| 127 | "Usage: websocket-client-coro <host> <port> <text>\n" << |
| 128 | "Example:\n" << |
| 129 | " websocket-client-coro echo.websocket.org 80 \"Hello, world!\"\n"; |
| 130 | return EXIT_FAILURE; |
| 131 | } |
| 132 | auto const host = argv[1]; |
| 133 | auto const port = argv[2]; |
| 134 | auto const text = argv[3]; |
| 135 | |
| 136 | // The io_context is required for all I/O |
| 137 | net::io_context ioc; |
| 138 | |
| 139 | // Launch the asynchronous operation |
| 140 | boost::asio::spawn(ioc, std::bind( |
| 141 | &do_session, |
| 142 | std::string(host), |
| 143 | std::string(port), |
| 144 | std::string(text), |
| 145 | std::ref(ioc), |
| 146 | std::placeholders::_1), |
| 147 | // on completion, spawn will call this function |
| 148 | [](std::exception_ptr ex) |
| 149 | { |
| 150 | // if an exception occurred in the coroutine, |
| 151 | // it's something critical, e.g. out of memory |
| 152 | // we capture normal errors in the ec |
| 153 | // so we just rethrow the exception here, |
| 154 | // which will cause `ioc.run()` to throw |
| 155 | if (ex) |
| 156 | std::rethrow_exception(ex); |
| 157 | }); |
| 158 | |
| 159 | // Run the I/O service. The call will return when |
| 160 | // the socket is closed. |
| 161 | ioc.run(); |
| 162 | |
| 163 | return EXIT_SUCCESS; |
| 164 | } |