如何连接到 "wss" [IBM Watson] 上的特定端点
How to connect to particular endpoint on "wss" [IBM Watson]
我正在使用 boost 库连接到 IBM 网络服务器(语音到文本)。
而且我卡在了网络套接字握手阶段。的代码。我已经提到了Web-socket async SSL client example。
这是代码(与上面的例子类似,中间有一些调试cout
):目标是连接到wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?apikey={my apikey}
// Report a failure
void
fail(beast::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// Sends a WebSocket message and prints the response
class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
websocket::stream<ssl::stream<tcp::socket>> ws_;
beast::multi_buffer buffer_;
std::string host_;
std::string text_;
public:
// Resolver and socket require an io_context
explicit
session(net::io_context& ioc, ssl::context& ctx)
: resolver_(ioc)
, ws_(ioc, ctx)
{
}
// Start the asynchronous operation
void run(char const* host, char const* port, char const* text)
{
// Save these for later
host_ = host;
text_ = text;
std::cout<< "Successfully reached: run operation:" << __LINE__<<" Resolving"<<std::endl;
// Look up the domain name
resolver_.async_resolve(
host,
port,
std::bind(&session::on_resolve, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}
void on_resolve(beast::error_code ec, tcp::resolver::results_type results)
{
if(ec)
return fail(ec, "resolve");
std::cout<< "Successfully reached: resolve operation:" << __LINE__<<" Resolving"<<std::endl;
// Make the connection on the IP address we get from a lookup
net::async_connect(
ws_.next_layer().next_layer(),
results.begin(),
results.end(),
std::bind(&session::on_connect, shared_from_this(), std::placeholders::_1));
}
void on_connect(beast::error_code ec)
{
if(ec)
return fail(ec, "connect");
std::cout<< "Successfully reached: handshaking operation:" << __LINE__<<" Handshaking"<<std::endl;
// Perform the SSL handshake
ws_.next_layer().async_handshake(ssl::stream_base::client, std::bind(&session::on_ssl_handshake, shared_from_this(), std::placeholders::_1));
}
void on_ssl_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "ssl_handshake");
std::cout<< "Successfully reached: handshaking operation:" << __LINE__<<" Handshaking"<<std::endl;
// Perform the websocket handshake
ws_.async_handshake(host_, "/speech-to-text/api/v1/recognize", std::bind( &session::on_handshake, shared_from_this(), std::placeholders::_1));
}
void on_handshake(beast::error_code ec)
{
std::cout<< "Successfully reached: handshaking operation:" << __LINE__<<" Handshaking"<<std::endl;
if(ec)
return fail(ec, "handshake");
std::cout<< "Successfully reached: handshaking operation:" << __LINE__<<" Send message"<<std::endl;
// Send the message
ws_.async_write(net::buffer(text_), std::bind(&session::on_write, shared_from_this(), std::placeholders::_1,
std::placeholders::_2));
}
void
on_write(
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec)
return fail(ec, "write");
// Read a message into our buffer
ws_.async_read(
buffer_,
std::bind(
&session::on_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
void
on_read(
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec)
return fail(ec, "read");
// Close the WebSocket connection
ws_.async_close(websocket::close_code::normal,
std::bind(
&session::on_close,
shared_from_this(),
std::placeholders::_1));
}
void
on_close(beast::error_code ec)
{
if(ec)
return fail(ec, "close");
// If we get here then the connection is closed gracefully
// The buffers() function helps print a ConstBufferSequence
std::cout << beast::buffers(buffer_.data()) << std::endl;
}
};
//------------------------------------------------------------------------------
int main(int argc, char** argv)
{
auto const host = "stream.watsonplatform.net";
auto const port = "443";
auto const text = "speech-to-text/api/v1/recognize";//apikey={my apikey}
std::cout<< "Successfully reached: Main function"<<std::endl;
// The io_context is required for all I/O
net::io_context ioc;
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23_client};
std::cout<< "Successfully reached: SSL context to hold certificates"<<std::endl;
// This holds the root certificate used for verification
load_root_certificates(ctx);
std::cout<< "Successfully reached: Loading root certificates - Goes to Launch asyn operation"<<std::endl;
// Launch the asynchronous operation
std::make_shared<session>(ioc, ctx)->run(host, port, text);
std::cout<< "Successfully reached: Launched asyn operation"<<std::endl;
// Run the I/O service. The call will return when
// the socket is closed.
ioc.run();
return EXIT_SUCCESS;
}
我在控制台上得到的输出为:
Successfully reached: Main function
Successfully reached: SSL context to hold certificates
Successfully reached: Loading root certificates - Goes to Launch asyn
operation
Successfully reached: run operation:71 Resolving
Successfully reached: Launched asyn operation
Successfully reached: resolve operation:84 Resolving
Successfully reached: handshaking operation:98 Handshaking
Successfully reached: handshaking operation:108 Handshaking
Successfully reached: handshaking operation:115 Handshaking
handshake: The WebSocket handshake was declined by the remote peer
在这里我有点困惑错误发生在on_ssl_handshake
或on_handshake
以及如何完成上述 "Aim".
请提供解决方案。
问候和感谢
Wireshark 条目:
哦,成功了!
问题出在 ws_.async_handshake
而不是我不得不使用 ws_.async_handshake_ex
如下:
ws_.async_handshake_ex(host_, "/speech-to-text/api/v1/recognize",[](request_type& reqHead){reqHead.insert(http::field::authorization,"Bearer {my token}");},std::bind( &session::on_handshake, shared_from_this(), std::placeholders::_1));
token
与 apikey
不同。它需要按照 this post
中给出的方式生成
澄清:
url 是 "stream.watsonplatform.net/speech-to-text/api/v1/recognize"
并且 authentication token
按照要求进行 header.
我正在使用 boost 库连接到 IBM 网络服务器(语音到文本)。
而且我卡在了网络套接字握手阶段。的代码。我已经提到了Web-socket async SSL client example。
这是代码(与上面的例子类似,中间有一些调试cout
):目标是连接到wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?apikey={my apikey}
// Report a failure
void
fail(beast::error_code ec, char const* what)
{
std::cerr << what << ": " << ec.message() << "\n";
}
// Sends a WebSocket message and prints the response
class session : public std::enable_shared_from_this<session>
{
tcp::resolver resolver_;
websocket::stream<ssl::stream<tcp::socket>> ws_;
beast::multi_buffer buffer_;
std::string host_;
std::string text_;
public:
// Resolver and socket require an io_context
explicit
session(net::io_context& ioc, ssl::context& ctx)
: resolver_(ioc)
, ws_(ioc, ctx)
{
}
// Start the asynchronous operation
void run(char const* host, char const* port, char const* text)
{
// Save these for later
host_ = host;
text_ = text;
std::cout<< "Successfully reached: run operation:" << __LINE__<<" Resolving"<<std::endl;
// Look up the domain name
resolver_.async_resolve(
host,
port,
std::bind(&session::on_resolve, shared_from_this(), std::placeholders::_1, std::placeholders::_2));
}
void on_resolve(beast::error_code ec, tcp::resolver::results_type results)
{
if(ec)
return fail(ec, "resolve");
std::cout<< "Successfully reached: resolve operation:" << __LINE__<<" Resolving"<<std::endl;
// Make the connection on the IP address we get from a lookup
net::async_connect(
ws_.next_layer().next_layer(),
results.begin(),
results.end(),
std::bind(&session::on_connect, shared_from_this(), std::placeholders::_1));
}
void on_connect(beast::error_code ec)
{
if(ec)
return fail(ec, "connect");
std::cout<< "Successfully reached: handshaking operation:" << __LINE__<<" Handshaking"<<std::endl;
// Perform the SSL handshake
ws_.next_layer().async_handshake(ssl::stream_base::client, std::bind(&session::on_ssl_handshake, shared_from_this(), std::placeholders::_1));
}
void on_ssl_handshake(beast::error_code ec)
{
if(ec)
return fail(ec, "ssl_handshake");
std::cout<< "Successfully reached: handshaking operation:" << __LINE__<<" Handshaking"<<std::endl;
// Perform the websocket handshake
ws_.async_handshake(host_, "/speech-to-text/api/v1/recognize", std::bind( &session::on_handshake, shared_from_this(), std::placeholders::_1));
}
void on_handshake(beast::error_code ec)
{
std::cout<< "Successfully reached: handshaking operation:" << __LINE__<<" Handshaking"<<std::endl;
if(ec)
return fail(ec, "handshake");
std::cout<< "Successfully reached: handshaking operation:" << __LINE__<<" Send message"<<std::endl;
// Send the message
ws_.async_write(net::buffer(text_), std::bind(&session::on_write, shared_from_this(), std::placeholders::_1,
std::placeholders::_2));
}
void
on_write(
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec)
return fail(ec, "write");
// Read a message into our buffer
ws_.async_read(
buffer_,
std::bind(
&session::on_read,
shared_from_this(),
std::placeholders::_1,
std::placeholders::_2));
}
void
on_read(
beast::error_code ec,
std::size_t bytes_transferred)
{
boost::ignore_unused(bytes_transferred);
if(ec)
return fail(ec, "read");
// Close the WebSocket connection
ws_.async_close(websocket::close_code::normal,
std::bind(
&session::on_close,
shared_from_this(),
std::placeholders::_1));
}
void
on_close(beast::error_code ec)
{
if(ec)
return fail(ec, "close");
// If we get here then the connection is closed gracefully
// The buffers() function helps print a ConstBufferSequence
std::cout << beast::buffers(buffer_.data()) << std::endl;
}
};
//------------------------------------------------------------------------------
int main(int argc, char** argv)
{
auto const host = "stream.watsonplatform.net";
auto const port = "443";
auto const text = "speech-to-text/api/v1/recognize";//apikey={my apikey}
std::cout<< "Successfully reached: Main function"<<std::endl;
// The io_context is required for all I/O
net::io_context ioc;
// The SSL context is required, and holds certificates
ssl::context ctx{ssl::context::sslv23_client};
std::cout<< "Successfully reached: SSL context to hold certificates"<<std::endl;
// This holds the root certificate used for verification
load_root_certificates(ctx);
std::cout<< "Successfully reached: Loading root certificates - Goes to Launch asyn operation"<<std::endl;
// Launch the asynchronous operation
std::make_shared<session>(ioc, ctx)->run(host, port, text);
std::cout<< "Successfully reached: Launched asyn operation"<<std::endl;
// Run the I/O service. The call will return when
// the socket is closed.
ioc.run();
return EXIT_SUCCESS;
}
我在控制台上得到的输出为:
Successfully reached: Main function
Successfully reached: SSL context to hold certificates
Successfully reached: Loading root certificates - Goes to Launch asyn operation
Successfully reached: run operation:71 Resolving
Successfully reached: Launched asyn operation
Successfully reached: resolve operation:84 Resolving
Successfully reached: handshaking operation:98 Handshaking
Successfully reached: handshaking operation:108 Handshaking
Successfully reached: handshaking operation:115 Handshaking
handshake: The WebSocket handshake was declined by the remote peer
在这里我有点困惑错误发生在on_ssl_handshake
或on_handshake
以及如何完成上述 "Aim".
请提供解决方案。
问候和感谢
Wireshark 条目:
哦,成功了!
问题出在 ws_.async_handshake
而不是我不得不使用 ws_.async_handshake_ex
如下:
ws_.async_handshake_ex(host_, "/speech-to-text/api/v1/recognize",[](request_type& reqHead){reqHead.insert(http::field::authorization,"Bearer {my token}");},std::bind( &session::on_handshake, shared_from_this(), std::placeholders::_1));
token
与 apikey
不同。它需要按照 this post
澄清:
url 是 "stream.watsonplatform.net/speech-to-text/api/v1/recognize"
并且 authentication token
按照要求进行 header.