xref: /aosp_15_r20/external/libwebsockets/minimal-examples/README.md (revision 1c60b9aca93fdbc9b5f19b2d2194c91294b22281)
1*1c60b9acSAndroid Build Coastguard Worker|name|demonstrates|
2*1c60b9acSAndroid Build Coastguard Worker---|---
3*1c60b9acSAndroid Build Coastguard Workerclient-server|Minimal examples providing client and server connections simultaneously
4*1c60b9acSAndroid Build Coastguard Workercrypto|Minimal examples related to using lws crypto apis
5*1c60b9acSAndroid Build Coastguard Workerdbus-server|Minimal examples showing how to integrate DBUS into lws event loop
6*1c60b9acSAndroid Build Coastguard Workerhttp-client|Minimal examples providing an http client
7*1c60b9acSAndroid Build Coastguard Workerhttp-server|Minimal examples providing an http server
8*1c60b9acSAndroid Build Coastguard Workerraw|Minimal examples related to adopting raw file or socket descriptors into the event loop
9*1c60b9acSAndroid Build Coastguard Workersecure-streams|Minimal examples related to the Secure Streams client api
10*1c60b9acSAndroid Build Coastguard Workerws-client|Minimal examples providing a ws client
11*1c60b9acSAndroid Build Coastguard Workerws-server|Minimal examples providing a ws server (and an http server)
12*1c60b9acSAndroid Build Coastguard Worker
13*1c60b9acSAndroid Build Coastguard Worker## FAQ
14*1c60b9acSAndroid Build Coastguard Worker
15*1c60b9acSAndroid Build Coastguard Worker### Getting started
16*1c60b9acSAndroid Build Coastguard Worker
17*1c60b9acSAndroid Build Coastguard WorkerBuild and install lws itself first (note that after installing lws on \*nix, you need to run `ldconfig` one time so the OS can learn about the new library.  Lws installs in `/usr/local` by default, Debian / Ubuntu ldconfig knows to look there already, but Fedora / CentOS need you to add the line `/usr/local/lib` to `/etc/ld.so.conf` and run ldconfig)
18*1c60b9acSAndroid Build Coastguard Worker
19*1c60b9acSAndroid Build Coastguard WorkerThen start with the simplest:
20*1c60b9acSAndroid Build Coastguard Worker
21*1c60b9acSAndroid Build Coastguard Worker`http-server/minimal-http-server`
22*1c60b9acSAndroid Build Coastguard Worker
23*1c60b9acSAndroid Build Coastguard Worker### Why are most of the sources split into a main C file file and a protocol file?
24*1c60b9acSAndroid Build Coastguard Worker
25*1c60b9acSAndroid Build Coastguard WorkerLws supports three ways to implement the protocol callback code:
26*1c60b9acSAndroid Build Coastguard Worker
27*1c60b9acSAndroid Build Coastguard Worker - you can just add it all in the same source file
28*1c60b9acSAndroid Build Coastguard Worker
29*1c60b9acSAndroid Build Coastguard Worker - you can separate it as these examples do, and #include it
30*1c60b9acSAndroid Build Coastguard Worker   into the main sources
31*1c60b9acSAndroid Build Coastguard Worker
32*1c60b9acSAndroid Build Coastguard Worker - you can build it as a standalone plugin that is discovered
33*1c60b9acSAndroid Build Coastguard Worker   and loaded at runtime.
34*1c60b9acSAndroid Build Coastguard Worker
35*1c60b9acSAndroid Build Coastguard WorkerThe way these examples are structured, you can easily also build
36*1c60b9acSAndroid Build Coastguard Workerthe protocol callback as a plugin just with a different
37*1c60b9acSAndroid Build Coastguard WorkerCMakeLists.txt... see https://github.com/warmcat/libwebsockets/tree/master/plugin-standalone
38*1c60b9acSAndroid Build Coastguard Workerfor an example.
39*1c60b9acSAndroid Build Coastguard Worker
40*1c60b9acSAndroid Build Coastguard Worker### Why would we want the protocol as a plugin?
41*1c60b9acSAndroid Build Coastguard Worker
42*1c60b9acSAndroid Build Coastguard WorkerYou will notice a lot of the main C code is the same boilerplate
43*1c60b9acSAndroid Build Coastguard Workerrepeated for each example.  The actual interesting part is in
44*1c60b9acSAndroid Build Coastguard Workerthe protocol callback only.
45*1c60b9acSAndroid Build Coastguard Worker
46*1c60b9acSAndroid Build Coastguard WorkerLws provides (-DLWS_WITH_LWSWS=1) a generic lightweight server app called 'lwsws' that
47*1c60b9acSAndroid Build Coastguard Workercan be configured by JSON.  Combined with your protocol as a plugin,
48*1c60b9acSAndroid Build Coastguard Workerit means you don't actually have to make a special server "app"
49*1c60b9acSAndroid Build Coastguard Workerpart, you can just use lwsws and pass per-vhost configuration
50*1c60b9acSAndroid Build Coastguard Workerfrom JSON into your protocol.  (Of course in some cases you have
51*1c60b9acSAndroid Build Coastguard Workeran existing app you are bolting lws on to, then you don't care
52*1c60b9acSAndroid Build Coastguard Workerabout this for that particular case).
53*1c60b9acSAndroid Build Coastguard Worker
54*1c60b9acSAndroid Build Coastguard WorkerBecause lwsws has no dependency on whatever your plugin does, it
55*1c60b9acSAndroid Build Coastguard Workercan mix and match different protocols randomly without needing any code
56*1c60b9acSAndroid Build Coastguard Workerchanges.  It reduces the size of the task to just writing the
57*1c60b9acSAndroid Build Coastguard Workercode you care about in your protocol handler, and nothing else to write
58*1c60b9acSAndroid Build Coastguard Workeror maintain.
59*1c60b9acSAndroid Build Coastguard Worker
60*1c60b9acSAndroid Build Coastguard WorkerLwsws supports advanced features like reload, where it starts a new server
61*1c60b9acSAndroid Build Coastguard Workerinstance with changed config or different plugins, while keeping the old
62*1c60b9acSAndroid Build Coastguard Workerinstance around until the last connection to it closes.
63*1c60b9acSAndroid Build Coastguard Worker
64*1c60b9acSAndroid Build Coastguard Worker### I get why there is a pss, but why is there a vhd?
65*1c60b9acSAndroid Build Coastguard Worker
66*1c60b9acSAndroid Build Coastguard WorkerThe pss is instantiated per-connection.  But there are almost always
67*1c60b9acSAndroid Build Coastguard Workerother variables that have a lifetime longer than a single connection.
68*1c60b9acSAndroid Build Coastguard Worker
69*1c60b9acSAndroid Build Coastguard WorkerYou could make these variables "filescope" one-time globals, but that
70*1c60b9acSAndroid Build Coastguard Workermeans your protocol cannot instantiate multiple times.
71*1c60b9acSAndroid Build Coastguard Worker
72*1c60b9acSAndroid Build Coastguard WorkerLws supports vhosts (virtual hosts), for example both https://warmcat.com
73*1c60b9acSAndroid Build Coastguard Workerand https://libwebsockets are running on the same lwsws instance on the
74*1c60b9acSAndroid Build Coastguard Workersame server and same IP... each of these is a separate vhost.
75*1c60b9acSAndroid Build Coastguard Worker
76*1c60b9acSAndroid Build Coastguard WorkerYour protocol may be enabled on multiple vhosts, each of these vhosts
77*1c60b9acSAndroid Build Coastguard Workerprovides a different vhd specific to the protocol instance on that
78*1c60b9acSAndroid Build Coastguard Workervhost.  For example many of the samples keep a linked-list head to
79*1c60b9acSAndroid Build Coastguard Workera list of live pss in the vhd... that means it's cleanly a list of
80*1c60b9acSAndroid Build Coastguard Workerpss opened **on that vhost**.  If another vhost has the protocol
81*1c60b9acSAndroid Build Coastguard Workerenabled, connections to that will point to a different vhd, and the
82*1c60b9acSAndroid Build Coastguard Workerlinked-list head on that vhd will only list connections to his vhost.
83*1c60b9acSAndroid Build Coastguard Worker
84*1c60b9acSAndroid Build Coastguard WorkerThe example "ws-server/minimal-ws-server-threads" demonstrates how to deliver
85*1c60b9acSAndroid Build Coastguard Workerexternal configuration data to a specific vhost + protocol
86*1c60b9acSAndroid Build Coastguard Workercombination using code.  In lwsws, this is simply a matter of setting
87*1c60b9acSAndroid Build Coastguard Workerthe desired JSON config.
88*1c60b9acSAndroid Build Coastguard Worker
89*1c60b9acSAndroid Build Coastguard Worker
90