4

我正在尝试使用 libwebsockets 创建一个 C++ websocket 客户端,但由于超时而无法建立连接。我已经剥离了一些东西进行测试,这就是我用来建立连接的东西:

协议

static int defaultCallback(
    struct  libwebsocket_context* context,
    struct  libwebsocket* wsi,
    enum    libwebsocket_callback_reasons reason,
    void*   user,
    void*   in,
    size_t  len)
{
    return 0;
}

static struct libwebsocket_protocols protocols[] =  {
    { "default", defaultCallback, 0 },
    { NULL, NULL, 0 }
};

创建上下文

    struct lws_context_creation_info info;
    memset(&info, 0, sizeof(info));
    info.port = CONTEXT_PORT_NO_LISTEN;
#ifndef LWS_NO_EXTENSIONS
    info.extensions = libwebsocket_get_internal_extensions();
#endif
    info.gid = -1;
    info.uid = -1;
    info.protocols = protocols;

    _context = libwebsocket_create_context(&info);

创建客户端

注意:地址“ws://localhost”我也试过“ws://echo.websocket.org”。localhost 服务器是一个 Node & ws 应用程序,我已经使用 Chrome 进行了测试并且运行良好。

_websocket = libwebsocket_client_connect(_context, // context
                                         _address.c_str(), // address
                                         _port, // port
                                         0, // use ssl?
                                         "/", // path
                                         _address.c_str(), // host
                                         NULL, // origin
                                         NULL,  // protocol
                                         -1);   // version

服务上下文

while(1) {
    libwebsocket_service(_context, 50);
}

输出 当我运行上面的代码时,这是我通过 libwebsockets 日志回调得到的输出:

NOTICE: Initial logging level 1023
NOTICE: Library version: 1.4 3ae1bad
NOTICE: IPV6 not compiled in
NOTICE: libev support not compiled in
INFO:  LWS_MAX_HEADER_LEN: 1024
INFO:  LWS_MAX_PROTOCOLS: 5
INFO:  SPEC_LATEST_SUPPORTED: 13
INFO:  AWAITING_TIMEOUT: 5
INFO:  SYSTEM_RANDOM_FILEPATH: '/dev/urandom'
INFO:  LWS_MAX_ZLIB_CONN_BUFFER: 65536
NOTICE:  static allocation: 4536 + (16 x 10240 fds) = 168376 bytes
INFO:  LWS_MAX_EXTENSIONS_ACTIVE: 3
NOTICE:  canonical_hostname = an-iMac
NOTICE:  per-conn mem: 248 + 2140 headers + protocol rx buf
PARSER:   Protocol: default
CLIENT: libwebsocket_client_connect: direct conn
CLIENT: libwebsocket_client_connect_2
CLIENT: libwebsocket_client_connect_2: address ws://localhost
INFO: insert_wsi_socket_into_fds: wsi=0x7ff808514c70, sock=8, fds pos=1
CLIENT: nonblocking connect retry
INFO: TIMEDOUT WAITING on 2
DEBUG: close: just_kill_connection
INFO: remove_wsi_socket_from_fds: wsi=0x7ff808514c70, sock=8, fds pos=1
DEBUG: Connection closed before server reply

我查看了所有我可以使用的 libwebsockets 示例/文档,但无法获得有关如何解决此问题的任何信息。任何帮助将不胜感激,并阻止我通过显示器。

4

2 回答 2

0

注意端口设置,如果使用端口协议,则不能单独指定端口。端口 8090 将在这里被 443 覆盖,不要这样做:

# WRONG
./lws_test_client wss://192.168.202.120 --port=8090

代替,将端口指定为 url 的一部分:

./lws_test_client wss://192.168.202.120:8090

细节...

我很难确定来自 libwebsockets 客户端的初始握手尝试发生了什么。观察浏览器(通过 chrome 扩展或 websockets.org 的 echo 服务)和节点 ws 服务器之间的健康会话,我得到了大量的握手包:

在此处输入图像描述

从 libwebsockets 客户端,我看不到任何从客户端发送的数据包。

以root身份运行没有帮助。我可以远程登录到服务器,至少可以看到一点流量。测试客户端出了问题。

我正在使用来自 master 分支的最新代码,也许这是问题的一部分,接下来我将切换回旧版本。

更新:问题在这里解决了。我在他的邮件列表中从 libwebsockets 作者 Andy 本人那里得到了帮助。他更正了我在命令行中对客户端端口的规范。一旦我正确指定了端口,我就有了一个连接客户端。这是对该问答的另一个答案的更深入解释,实际上是在说同样的事情。我希望这是和你一样的问题。

# DO NOT USE: ./lws_test_client wss://192.168.202.120 --port=8090
# "wss://" will override the specific port provided!
# INSTEAD, specify it as part of the url.
./lws_test_client wss://192.168.202.120:8090
于 2016-06-17T18:26:01.213 回答
0

尝试不带“ws://”的地址。只是简单的“localhost”或“127.0.0.1”。

于 2015-09-29T23:04:18.000 回答