[Libwebsockets] how to close Websocket connection string other than call back

Andy Green andy at warmcat.com
Tue Jan 12 12:09:57 CET 2016



On January 12, 2016 11:01:04 AM GMT+00:00, Subi S S <subi.s at cambiumnetworks.com> wrote:
>Hi ,
>
>I am using lws_context_destroy(context) to close the connection from
>client code ( for me it is not possible to return -1 from call back),

It's really 'not possible'?  How about set a flag that you want to close, then ask for callback when writeable and return -1 there.

>is there a way to send reason string in this case ? or any other API is
>there to close the connection ?
>
>But lws_context_destroy invokes close with 
>LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY

Yeah... you're having the problem because you're mixing the desire for a controlled close with what's basically an emergency cleanup because you're exiting.

-Andy

>
>for (n = 0; n < context->fds_count; n++) {
>            struct lws *wsi = wsi_from_fd(context, context->fds[n].fd);
>              if (!wsi)
>                     continue;
>      lws_close_free_wsi(wsi, LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY
>                           /* no protocol close */);
>              n--;
>       }
>
>................................. but in lws_close_free_wsi reason is
>ignored for "LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY"
>
>if (old_state == LWSS_ESTABLISHED &&
>          (wsi->u.ws.close_in_ping_buffer_len || /* already a reason */
>            (reason != LWS_CLOSE_STATUS_NOSTATUS &&
>            (reason != LWS_CLOSE_STATUS_NOSTATUS_CONTEXT_DESTROY)))) {
>              lwsl_debug("sending close indication...\n");
>
>            /* if no prepared close reason, use 1000 and no aux data */
>              if (!wsi->u.ws.close_in_ping_buffer_len) {
>                     wsi->u.ws.close_in_ping_buffer_len = 2;
>              wsi->u.ws.ping_payload_buf[LWS_SEND_BUFFER_PRE_PADDING] =
>                           (reason >> 16) & 0xff;
>          wsi->u.ws.ping_payload_buf[LWS_SEND_BUFFER_PRE_PADDING + 1] =
>                           reason & 0xff;
>              }
>
>
>
>Thanks,
>Subi
>
>
>------------------------------------------------------------------------
>
>_______________________________________________
>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org
>http://ml.libwebsockets.org/mailman/listinfo/libwebsockets




More information about the Libwebsockets mailing list