所以我只是碰巧尝试了同步的例子,它是连接的。在客户端 hello 数据包中,它与我们的代码之间的少量差异之一是 SNI / 服务器名称字符串;Server Name: xxx.azurewebsites.net:443
与Server Name: xxx.azurewebsites.net
。
它碰巧在我们的嵌入式代码中包含了该端口,但不是来自该示例。我们的代码恰好基于异步客户端示例。
异步示例有;
// Update the host_ string. This will provide the value of the
// Host HTTP header during the WebSocket handshake.
// See https://tools.ietf.org/html/rfc7230#section-5.4
host_ += ':' + std::to_string(ep.port());
// Set a timeout on the operation
beast::get_lowest_layer(ws_).expires_after(std::chrono::seconds(30));
// Set SNI Hostname (many hosts need this to handshake successfully)
if(! SSL_set_tlsext_host_name(
ws_.next_layer().native_handle(),
host_.c_str()))
{
ec = beast::error_code(static_cast<int>(::ERR_get_error()),
net::error::get_ssl_category());
return fail(ec, "connect");
}
同步示例有;
// Set SNI Hostname (many hosts need this to handshake successfully)
if(! SSL_set_tlsext_host_name(ws.next_layer().native_handle(), host.c_str()))
throw beast::system_error(
beast::error_code(
static_cast<int>(::ERR_get_error()),
net::error::get_ssl_category()),
"Failed to set SNI Hostname");
// Update the host_ string. This will provide the value of the
// Host HTTP header during the WebSocket handshake.
// See https://tools.ietf.org/html/rfc7230#section-5.4
host += ':' + std::to_string(ep.port());
所以基本上,这些示例的工作方式不同,无论是否将端口添加到 SNI。在 github 上查找 boost beast,表明异步示例已在develop中修复;他们选择不包括端口号。