4

I'm trying to create a C++ websocket client using libwebsockets but I can't establish a connection due to it timing out. I've stripped things down for testing and here's what I'm using to establish a connection:

Protocols

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 }
};

Create Context

    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);

Create Client

NOTE: The address "ws://localhost" I've also tried "ws://echo.websocket.org". The localhost server is a Node & ws app that I've tested with Chrome and works perfectly.

_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

Service The Context

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

Output When I run the above this is the output I get through the libwebsockets logging callback:

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

I've looked at all the libwebsockets examples/documentation I can get my hands on and can't get any info on how to resolve this. Any help would be greatly appreciated and prevent me from putting my head through my monitor.

Huhwha
  • 575
  • 5
  • 15
  • What port? Some low number ports are secured. – Richard Critten May 19 '15 at 15:45
  • I've tried a variety of ports: 80, 8080, 9000, 3333, etc. As I mentioned, with the localhost I have a server running that I'm able to connect to with a browser-based websocket client, so the port and address in theory should work. – Huhwha May 19 '15 at 16:40
  • I feel like I wrote this question, I'm in exactly the same boat. I stood up a node ws server and hit it with chrome extensions and the websocket.org's echo form successfully. I'm running libwebsockets in debug but it just posts "connection closed before server reply" / LWS_CALLBACK_CLIENT_CONNECTION_ERROR. I guess it's time to fire up wireshark. – moodboom Jun 17 '16 at 17:43
  • I should add that the node ws server never receives a connection attempt, that is, it never reaches [wsserver.on('connection'...] – moodboom Jun 17 '16 at 17:51

2 Answers2

0

Try address without "ws://". Just plain "localhost", or "127.0.0.1".

user3621991
  • 126
  • 1
0

Be careful with port settings, you cannot specify the port separately if you use a port protocol. Port 8090 will be overridded with 443 here, don't do it this way:

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

INSTEAD, specify the port as part of the url:

./lws_test_client wss://192.168.202.120:8090

Details...

It's been really hard for me to determine what's going on with the initial handshake attempt from a libwebsockets client. Watching a healthy session between a browser (via a chrome extension or websockets.org's echo service) and a node ws server, I get a large number of handshaking packets:

enter image description here

From libwebsockets client, I don't see any packet being sent from the client at ALL.

Running as root doesn't help. I can telnet to the server and at least see a little traffic to it. Something is wrong with the test client out of the gate.

I'm using the latest code from the master branch, maybe that's part of the problem, I'm going to switch back to an older release next.

UPDATE: Problem solved here. I got help from Andy himself, the libwebsockets author, on his mailing list. He corrected my specification of the client port in my command line. Once I specified the port correctly, I had a connecting client. This is the deeper explanation of the other answer on this Q&A, which in effect said the same thing. I hope it is the same issue as you.

# 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
moodboom
  • 6,225
  • 2
  • 41
  • 45