Spritetm / libesphttpd

NOTE: THIS CODE IS UNMAINTAINED. Please take a look at https://github.com/chmorgan/libesphttpd instead.
125 stars 109 forks source link

Websocket remains open after refresh #4

Closed someburner closed 8 years ago

someburner commented 8 years ago

I've been playing around with your websockets implementation and I found that, at least with my setup, the socket connections were not being cleaned up after a closed connection. For instance, if I hit refresh, browse away, etc, and then try to create a new socket, Chrome would throw an error saying "invalid status line" or something of that sort.

I traced it down to the end of the cgiWebsocket() method in cgiwebsocket.c:

    //Sending is done. Call the sent callback if we have one.
    Websock *ws=(Websock*)connData->cgiPrivData;
    if (ws && ws->sentCb) ws->sentCb(ws);

    if (ws && ws->priv->mustClose) return HTTPD_CGI_DONE;

    return HTTPD_CGI_MORE;

After 1 or 2 connections it forever returns HTTPD_CGI_DONE, but never gets to the cleanup code on a terminated connection.

I tweaked it to close the connection and then let it continue to HTTPD_CGI_MORE and then on the next go-around it detects the null connection and cleans up. Like so:

    if (ws && ws->priv->mustClose) {
        cgiWebsocketClose(ws);
        // return HTTPD_CGI_DONE;
    }
    return HTTPD_CGI_MORE;

After doing this it seems to work just fine.

I could be doing something fundamentally wrong here as I'm not too familiar with websockets, but my static html page creates the WS on a button click, so I'm pretty sure I'm not generating multiple.

Spritetm commented 8 years ago

Fixed, thanks for reporting!