2 * lws-minimal-ws-server
4 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
6 * This file is made available under the Creative Commons CC0 1.0
7 * Universal Public Domain Dedication.
9 * This demonstrates a minimal ws server that can cooperate with
10 * other threads cleanly. Two other threads are started, which fill
11 * a ringbuffer with strings at 10Hz.
13 * The actual work and thread spawning etc are done in the protocol
14 * implementation in protocol_lws_minimal.c.
16 * To keep it simple, it serves stuff in the subdirectory "./mount-origin" of
17 * the directory it was started in.
18 * You can change that by changing mount.origin.
21 #include <libwebsockets.h>
26 #define LWS_PLUGIN_STATIC
27 #include "protocol_lws_minimal.c"
29 static struct lws_protocols protocols[] = {
30 { "http", lws_callback_http_dummy, 0, 0 },
31 LWS_PLUGIN_PROTOCOL_MINIMAL,
32 { NULL, NULL, 0, 0 } /* terminator */
35 static int interrupted;
37 static const struct lws_http_mount mount = {
38 /* .mount_next */ NULL, /* linked-list "next" */
39 /* .mountpoint */ "/", /* mountpoint URL */
40 /* .origin */ "./mount-origin", /* serve from dir */
41 /* .def */ "index.html", /* default filename */
44 /* .extra_mimetypes */ NULL,
45 /* .interpret */ NULL,
47 /* .cache_max_age */ 0,
49 /* .cache_reusable */ 0,
50 /* .cache_revalidate */ 0,
51 /* .cache_intermediaries */ 0,
52 /* .origin_protocol */ LWSMPRO_FILE, /* files in a dir */
53 /* .mountpoint_len */ 1, /* char count */
54 /* .basic_auth_login_file */ NULL,
58 * This demonstrates how to pass a pointer into a specific protocol handler
59 * running on a specific vhost. In this case, it's our default vhost and
60 * we pass the pvo named "config" with the value a const char * "myconfig".
62 * This is the preferred way to pass configuration into a specific vhost +
66 static const struct lws_protocol_vhost_options pvo_ops = {
69 "config", /* pvo name */
70 (void *)"myconfig" /* pvo value */
73 static const struct lws_protocol_vhost_options pvo = {
74 NULL, /* "next" pvo linked-list */
75 &pvo_ops, /* "child" pvo linked-list */
76 "lws-minimal", /* protocol name we belong to on this vhost */
80 void sigint_handler(int sig)
85 int main(int argc, const char **argv)
87 struct lws_context_creation_info info;
88 struct lws_context *context;
90 int logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
91 /* for LLL_ verbosity above NOTICE to be built into lws,
92 * lws must have been configured and built with
93 * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
94 /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
95 /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
98 signal(SIGINT, sigint_handler);
100 if ((p = lws_cmdline_option(argc, argv, "-d")))
103 lws_set_log_level(logs, NULL);
104 lwsl_user("LWS minimal ws server + threads | visit http://localhost:7681\n");
106 memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
108 info.mounts = &mount;
109 info.protocols = protocols;
110 info.pvo = &pvo; /* per-vhost options */
112 LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;
114 context = lws_create_context(&info);
116 lwsl_err("lws init failed\n");
120 /* start the threads that create content */
123 if (lws_service(context, 0))
126 lws_context_destroy(context);