c++ - 通过 C++ Boost Websockets 的 Watson 文本转语音 - "No such host is known"

标签 c++ boost websocket ibm-watson boost-beast

我一直无法使用 Boost Beast 库通过 C++ 中的 websockets 连接到 Watson 的文本转语音服务

我的代码可以在端口 80 上成功地与 echo.websocket.org 进行交互,但它不适用于 Watson 的 url。我已经尝试使用协议(protocol)的变体(http(s)、ws(s) 和未指定的(适用于 echo.websocket.com))并且我已经尝试了端口 80 和 443,只是为了确定。

我能够在 Javascript 中成功运行代码,并使用 Firefox 的内置网络工具,我已经验证它可以在端口 443 上工作。使用完全相同的 URL 和端口号给我以下信息:“没有这样的主机是已知的”

这里是正确建立连接的相关JS代码

var completeUrl = "wss://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?access_token=" + accessToken + "&voice=en-US_AllisonVoice";
socket = new WebSocket(completeUrl);

以下 C++ 代码在理论上可以正常工作,并且可以在端口 80 上与 echo.websocket.org 一起使用,但不能与 Watson 一起使用。

#include <boost/beast/core.hpp>
#include <boost/beast/websocket.hpp>
#include <boost/asio/connect.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <cstdlib>
#include <iostream>
#include <string>

using tcp = boost::asio::ip::tcp;               // from <boost/asio/ip/tcp.hpp>
namespace websocket = boost::beast::websocket;  // from <boost/beast/websocket.hpp>

// Sends a WebSocket message and prints the response
int main(int argc, char** argv)
{
    try
    {
        std::string accessToken = "XXXXX";
        auto const text = "The quick brown fox jumps over the lazy dog.";

        std::string baseURL = "wss://stream.watsonplatform.net/text-to-speech/api/v1/synthesize";
        std::string voiceModel = "en-US_AllisonVoice";
        auto const port = "443";    // port 80 for echo.websocket.org
                                    // port 443 for watson

        std::string const host = baseURL + "?access_token=" + accessToken + "&voice=" + voiceModel;
        //std::string const host = "echo.websocket.org";

        boost::asio::io_context ioc;
        tcp::resolver resolver{ ioc };
        websocket::stream<tcp::socket> ws{ ioc };

        auto const results = resolver.resolve(host, port);      // Problem line - "resolve: No such host is known"
        std::cout << "Host resolved" << std::endl;

        boost::asio::connect(ws.next_layer(), results.begin(), results.end());
        ws.handshake(host, "/");
        std::cout << "Connection established" << std::endl << "------------------------------" << std::endl;

        ws.write(boost::asio::buffer(std::string(text)));
        std::cout << "Client request: " << text << std::endl;
        boost::beast::multi_buffer buffer;
        ws.read(buffer);
        ws.close(websocket::close_code::normal);

        std::cout << "Server response: " << boost::beast::buffers(buffer.data()) << std::endl;
    }
    catch (std::exception const& e)
    {
        std::cerr << "Error: " << e.what() << std::endl;
        return EXIT_FAILURE;
    }
    return EXIT_SUCCESS;
}

如果 Watson URL 错误,它就不能在 Javascript 中运行。如果 C++ 代码有误,它就不能与 echo.websocket.org 一起使用。所以我不知道问题是什么。

最佳答案

代码中的 baseURL 指定“wss”,表示安全 Websockets (SSL)。但是您的 stream 被声明为纯流。如果你想连接到安全的 websocket 服务器,你应该将你的代码基于 websocket-client-ssl 示例: https://github.com/boostorg/beast/blob/d43d9421a40c0251614bc45ea6dcf921a3dbaf37/example/websocket/client/sync-ssl/websocket_client_sync_ssl.cpp#L64

关于c++ - 通过 C++ Boost Websockets 的 Watson 文本转语音 - "No such host is known",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54698577/

相关文章:

C++构造函数调用非虚函数,比如说funcA,但是funcA调用了虚函数,有危险吗

c++ - 数组索引越界,但 gdb 报告错误行 - 为什么?

c++ - 调用 std::map operator[] 或插入时会发生什么

Android phonegap 和 SockJS

c++ - c++ 中的静态成员函数是否在多个翻译单元中复制?

c++ - 使用 boost bind 时对非静态成员函数的使用无效 - C++

c++ - 为什么在使用 boost::copy_exception 时会丢失类型信息?

c++ - 使用输入参数初始化结构

Firefox Websocket 安全问题

node.js - Websocket - 等待 http 请求回调在下一个推送事件之前执行