首页 > 解决方案 > Watson Text-to-Speech via C++ Boost Websockets - “没有这样的主机是已知的”

问题描述

我无法使用 Boost Beast 库通过 C++ 中的 websockets 连接到 Watson 的 Text-To-Speech 服务

我的代码在端口 80 上与 echo.websocket.org 成功交互,但它不适用于 Watson 的 url。我尝试过使用协议的变体(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 一起使用。所以我不知道问题是什么。

标签: c++boostwebsocketibm-watsonboost-beast

解决方案


您的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


推荐阅读