C++ 如何使用C+连接到Watson语音到文本服务器+;RESTSDK Websocket
我正在尝试使用websocket连接到Watson Speech to Text server,但当我尝试建立连接时,没有连接。我获得了连接令牌来自动验证我的websocket连接,因此连接URL并尝试连接,但正如我之前所说的不要连接,有人能帮我吗 提前谢谢 这是我现在的代码:C++ 如何使用C+连接到Watson语音到文本服务器+;RESTSDK Websocket,c++,websocket,casablanca,cpprest-sdk,C++,Websocket,Casablanca,Cpprest Sdk,我正在尝试使用websocket连接到Watson Speech to Text server,但当我尝试建立连接时,没有连接。我获得了连接令牌来自动验证我的websocket连接,因此连接URL并尝试连接,但正如我之前所说的不要连接,有人能帮我吗 提前谢谢 这是我现在的代码: #include <cpprest/ws_client.h> #include <cpprest/http_client.h> #include <cpprest/filestream.h&
#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();
}
#包括
#包括
#包括
#包括
#包括
#包括
使用命名空间web;
使用名称空间web::websockets;
使用名称空间web::websockets::client;
使用名称空间web::http;
使用名称空间web::http::client;
#包括
#包括
使用命名空间实用程序;//常见的实用程序,如字符串转换
使用命名空间web;//常见的功能,如URI。
使用命名空间web::http;//通用HTTP功能
使用命名空间web::http::client;//HTTP客户端功能
使用命名空间并发::streams;//异步流
int main(int argc,char*argv[]){
auto fileStream=std::make_shared();
//打开流以输出文件。
pplx::task requestTask=fstream::open_ostream(U(“../data/token.txt”)。然后([=](ostream outFile)
{
*fileStream=outFile;
//创建http_客户端以发送请求。
http_client_config;
//生成请求URI并启动请求。
凭证凭证凭证(L“******-***-***-***-***-******-***-*************”,L“**********”;
配置。设置_凭证(cred);
http_客户端(U)https://stream.watsonplatform.net(pdf格式),;
uri\U生成器(U(“/authorization/api/v1/token?url=https://stream.watsonplatform.net/speech-to-text/api"));
返回client.request(方法::GET,builder.to_string());
})
//处理到达的响应头。
。然后([=](http_响应)
{
//printf(“收到的响应状态代码:%u\n”,response.status_code());
//将响应主体写入文件。
返回response.body();
})
//关闭文件流。
。然后([=](大小)
{
返回fileStream->close();
});
//等待所有未完成的I/O完成并处理任何异常
尝试
{
requestTask.wait();
}
捕获(const std::exception&e)
{
printf(“错误异常:%s\n”,例如what());
}
//阿尔基沃和瓜达尔代币酒店//////
std::ifstream文件;
字符串标记;
打开(“../data/token.txt”);
文件>>令牌;
std::字符串url=”wss://stream.watsonplatform.net/speech-to-text/api/v1/recognize?watson-token=“+token+”&model=pt-BR_宽带模型”;
uriwsurl(转换::到字符串(url));
websocket_客户端wsclient;
wsclient.connect(wsUrl.wait();
}