我正在尝试连接到 Watson Speech to Text 服务器 uisng websocket,但是当我尝试建立连接时没有连接。我获得了连接令牌来验证我的 websocket 连接,因此与 URL 连接并尝试连接,但正如我所说的那样,不要连接,有人可以帮助我吗?
提前致谢。
这是我现在的代码:
#include <cpprest/ws_client.h>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
#include <iostream>
#include <sstream>
#include <fstream>
using namespace web;
using namespace web::websockets;
using namespace web::websockets::client;
using namespace web::http;
using namespace web::http::client;
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
using namespace utility; // Common utilities like string conversions
using namespace web; // Common features like URIs.
using namespace web::http; // Common HTTP functionality
using namespace web::http::client; // HTTP client features
using namespace concurrency::streams; // Asynchronous streams
int main(int argc, char* argv[]){
auto fileStream = std::make_shared<ostream>();
// Open stream to output file.
pplx::task<void> requestTask = fstream::open_ostream(U("../data/token.txt")).then([=](ostream outFile)
{
*fileStream = outFile;
// Create http_client to send the request.
http_client_config config;
// Build request URI and start the request.
credentials cred(L"******-****-****-****-************", L"************");
config.set_credentials(cred);
http_client client(U("https://stream.watsonplatform.net"), config);
uri_builder builder(U("/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api"));
return client.request(methods::GET, builder.to_string());
})
// Handle response headers arriving.
.then([=](http_response response)
{
//printf("Received response status code:%u\n", response.status_code());
// Write response body into the file.
return response.body().read_to_end(fileStream->streambuf());
})
// Close the file stream.
.then([=](size_t)
{
return fileStream->close();
});
// Wait for all the outstanding I/O to complete and handle any exceptions
try
{
requestTask.wait();
}
catch (const std::exception &e)
{
printf("Error exception:%s\n", e.what());
}
//Ler arquivo e guardar token//////
std::ifstream file;
std::string token;
file.open("../data/token.txt");
file >> token;
std::string url = "wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=" + token + "&model=pt-BR_BroadbandModel";
uri wsUrl(conversions::to_string_t(url));
websocket_client wsclient;
wsclient.connect(wsUrl).wait();
}