[Libwebsockets] pointers for implementing websocket tcp gateway and a static http server

Andy Green andy at warmcat.com
Wed Oct 12 10:21:07 CEST 2016


On Wed, 2016-10-12 at 09:38 +0200, jsaak wrote:
> On 10/12/2016 09:25 AM, Andy Green wrote:
> > 
> > On Wed, 2016-10-12 at 09:16 +0200, jsaak wrote:
> > > 
> > > I have an app which works under linux/android/ios/osx/windows,
> > > and
> > > uses
> > > TCP/IP to communicate. I want to bundle a websocket to tcp
> > > gateway
> > > to
> > > the app so i can access it through a browser. Also i want to
> > > include
> > > a
> > > static http server.
> > > 
> > > I found libwebsockets, and i think it might be a solution.
> > 
> > Sounds like the right kind of task for lws.
> > 
> > > 
> > > All I ask of you is some advice. How to start with libwebsockets?
> > > It
> > > is
> > > a bit confusing for me.
> > 
> > Run the test server is the best way to get started.
> > 
> > The key question is "How does your existing app wait on network
> > events
> > at the moment"?  It's like poll() or like an event loop?
> > 
> > If it already has its own poll() management, you should look at
> > using
> > External Poll support to integrate lws with it.
> > 
> > If it uses an event loop, lws has libuv (preferred) and libev event
> > loop support, you can integrate using that.
> > 
> > How nice a ride you get is mainly depending on the existing app's
> > wait
> > for network events method.
> > 
> 
> I am using SDL_Net which uses select() on all platforms i think.
> But it may be irrelevant, since i want to put libwebsocket to a new 
> thread (so i can use any event loop there), and only communicate with
> it 
> through TCP/IP.

Fair enough...

> I think libuv is the way to go for me. So shall i start with 
> https://github.com/warmcat/libwebsockets/blob/master/test-server/test
> -server-v2.0.c 
> ?

Yes... either that or lwsws.

https://libwebsockets.org/lws-api-doc-master/html/md_README.lwsws.html

Lwsws will take care of all of the boilerplate and give you an lws
webserver configurable with JSON in one step.  Basically you can avoid
writing any code that is unrelated to your protocol.

> Do I have to make a default websocket protocol? And in that protocol
>> have to open/read/write/close the real TCP connection?

If you use test-server-v2.0.c style, you would copy the "main.c" type
stuff and write a plugin (initially, just copy dumb-increment plugin is
enough) that will hold your protocol actions.

You'll also need to set up mounts etc to serve the http stuff
programatically, it shows you how in the example code.

lwsws lets you configure the mounts and so on in JSON.  You only need
to provide the protocol plugin then which is only concerned with your
actions.

Any way you do it, lws takes care of socket-level stuff including
accept and close, without you needing to do anything.

It's best to take advantage of lwsws if your server is essentially
standalone.  https://libwebsockets.org (and https://warmcat.com) are
served from an Rpi 3 using lwsws.

-Andy


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



More information about the Libwebsockets mailing list