[Libwebsockets] Nodejs to libwebsockets

Andy Green andy at warmcat.com
Thu Apr 4 06:23:03 CEST 2019



On April 4, 2019 12:08:05 PM GMT+08:00, Paolo Denti <paolo.denti at gmail.com> wrote:
>Hi,
>I am trying to migrate the code below, from node.js to libwebsockets (i
>oversimplified it removing all the boilerplate code)
>Basically I need to connect, send some data and stay connected, forever
>
>I tried to find the closest option possible in the minimal examples but
>actually I am not able to replicate the behavior using any of the
>existing
>samples.

Well, they are minimal examples for particular cases.  For real code, you might have to mix and match.

>Could anyone point me to the right direction?

This shows how to do the 'nailed up client connection' and is a good starting point.

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/ws-client/minimal-ws-client-pmd-bulk/protocol_lws_minimal_pmd_bulk.c

>Thanks
>
>var Stomp = require('stompjs');
>
>var connect_callback = function() {
>client.send("/ws/xx/yy/zz", {}, JSON.stringify({token: sample
>data""}));
>};
>
>var client = Stomp.overWS('wss://<my server>/<my websocket endpoint>');
>client.connect({}, connect_callback);

It looks like this doesn't specify a subprotocol, which is pretty craptastic.

Lws can do it if you leave the client connection protocol NULL.

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/ws-client/minimal-ws-client-pmd-bulk/protocol_lws_minimal_pmd_bulk.c#n96

That example doesn't use tls... you'll need to set this accordingly

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/ws-client/minimal-ws-client-pmd-bulk/protocol_lws_minimal_pmd_bulk.c#n94

and the context / vhost options like this

https://libwebsockets.org/git/libwebsockets/tree/minimal-examples/ws-client/minimal-ws-client-echo/minimal-ws-client-echo.c#n141

-Andy


More information about the Libwebsockets mailing list