davidmoreno / onion

C library to create simple HTTP servers and Web Applications.
http://www.coralbits.com/libonion/
Other
2.01k stars 249 forks source link

Are websockets blocked? #273

Open eddyem opened 3 years ago

eddyem commented 3 years ago

I try to work with websockets but found that if one websocket is opened, I can't open another (in another tab, another browser or any other PC). How can I fix this bug for opening websockets to given port in any quantity?

davidmoreno commented 3 years ago

For mode ONION_ONE only one connection can be opened. Try with O_THREADED or O_DETACH_LISTEN at onion_new.

On Fri, 23 Oct 2020 at 15:23, Edward Emelianov notifications@github.com wrote:

I try to work with websockets but found that if one websocket is opened, I can't open another (in another tab, another browser or any other PC). How can I fix this bug for opening websockets to given port in any quantity?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/davidmoreno/onion/issues/273, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACOZOWY2PNQURK2RREXLS3SMF7ULANCNFSM4S4RXZNA .

-- David Moreno Montero

dmoreno@coralbits.com +34 658 18 77 17 [image: Coralbits.com] http://www.coralbits.com/ http://www.coralbits.com

eddyem commented 3 years ago

I run it with O_THREADED:

static void *runWS(_U_ void *data){
    ow = onion_new(O_THREADED);
    if(!(onion_flags(ow) & O_SSL_AVAILABLE)){
        ONION_ERROR("SSL support is not available");
        signals(1);
    }
    int error = onion_set_certificate(ow, O_SSL_CERTIFICATE_KEY, G.certfile, G.keyfile);
    if(error){
        ONION_ERROR("Can't set certificate and key files (%s, %s)", G.certfile, G.keyfile);
        signals(1);
    }
    onion_set_port(ow, G.wsport);
    onion_url *url = onion_root_url(ow);
    onion_url_add(url, "", websocket_run);
    DBG("Listen websocket");
    error = onion_listen(ow);
    if(error) ONION_ERROR("Can't create WEBSOCKET server: %s", strerror(errno));
    onion_free(ow);
    return NULL;
}

But when one websocket is opened, no other connections could be done. And this function don't run:

onion_connection_status websocket_run(_U_ void *data, onion_request *req, onion_response *res){
    FNAME();
    onion_websocket *ws = onion_websocket_new(req, res);
    if (!ws){
        red("OCS_PROCESSED");
        DBG("Processed");
        return OCS_PROCESSED;
    }
    DBG("WS ready");
    const char *host = onion_request_get_client_description(req);
    const char *UA = onion_request_get_header(req, "User-Agent");
    green("Got WS connection from %s (UA: %s)\n", host, UA);
    WSdata *wsdata = calloc(1, sizeof(WSdata));
    wsdata->flags = WS_FLAG_NOTAUTHORIZED;
    wsdata->IPhash = MurmurOAAT64(host);
    wsdata->UAhash = MurmurOAAT64(UA);
    onion_websocket_set_userdata(ws, (void*)wsdata, free);
    onion_websocket_set_callback(ws, websocket_cont);
    return OCS_WEBSOCKET;
}
eddyem commented 3 years ago

I have tried an websocket example from libonion. And it don't work too! As only I open one connection, I can't open another. What's the right way to work with websockets in libonion? I need at least 10-20 simultaneous connections.

davidmoreno commented 3 years ago

Hi,

sorry to hear.

Websockets support was not fully developed and suffer from problems. It should work with several threads, but it blocks the threads. A full rewrite of websockets would be needed, as more testing, but currently I have no time to do it, and it does not look like i will have it.

I would not recommend onion for a websocket application. Only for very small apps.

As always everybody is welcome to help, and patches are more than welcome.

Regards, David.

On Tue, 27 Oct 2020 at 09:43, Edward Emelianov notifications@github.com wrote:

I have tried an websocket example from libonion. And it don't work too! As only I open one connection, I can't open another. What's the right way to work with websockets in libonion? I need at least 10-20 simultaneous connections.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/davidmoreno/onion/issues/273#issuecomment-717082404, or unsubscribe https://github.com/notifications/unsubscribe-auth/AACOZOSHLYN3QN5RACQ3BV3SM2B4JANCNFSM4S4RXZNA .

-- David Moreno Montero

dmoreno@coralbits.com +34 658 18 77 17 [image: Coralbits.com] http://www.coralbits.com/ http://www.coralbits.com

YggdrasiI commented 3 years ago

websockets.c.txt Hello eddyem,

I've attached my adaption of the websocket example for multiple clients. It uses onion threaded o = onion_new(O_THREADED|O_NO_SIGTERM|O_DETACH_LISTEN);

and then pushs periodical test strings data to clients.

Edit: Upload better version. websockets.c.txt

eddyem commented 3 years ago

I found a problem: websockets didn't work for several clients due to gentoo's ebuild of libonion. When I use onion from git, it works, but with segfaults when client transmit some data. Also there's a problem with such little active clients (8 by default in libonion). This number should be at least several thousands!