[Libwebsockets] Client timing out

Mattia Romeo mromeo at giganticmechanic.com
Wed May 20 20:00:45 CEST 2015


Thanks so much for sending this - it held the key to unlocking the mystery. The problem was that I was including the protocol in the address string (i.e. I was using “ws://localhost” as opposed to “localhost”). None of the examples I saw included samples of the address but yours had a default value without the protocol which made the issue clear.

Thanks,
Mattia
On May 20, 2015 at 9:26:12 AM, Brice Hamon (andy.green at linaro.org) wrote:

Look at this little client program. It may help you. 

Not pretty but it works for me on windoz and Linux.

On Wed, May 20, 2015 at 9:03 AM, Mattia Romeo <andy.green at linaro.org> wrote:
I'm trying to create a C++ websocket client using libwebsockets but I can't establish a connection due to it timing out (I think - there may be other problems). 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.


Thanks,
Mattia

_______________________________________________
Libwebsockets mailing list
Libwebsockets at ml.libwebsockets.org
http://ml.libwebsockets.org/mailman/listinfo/libwebsockets


_______________________________________________  
Libwebsockets mailing list  
Libwebsockets at ml.libwebsockets.org  
http://ml.libwebsockets.org/mailman/listinfo/libwebsockets  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://libwebsockets.org/pipermail/libwebsockets/attachments/20150520/09b3d4fc/attachment-0001.html>


More information about the Libwebsockets mailing list