Anybody use web sockets?

Need help with C, C++, perl, python, etc?

Anybody use web sockets?

Postby ab1jx » 2021-02-15 23:27

I like the way they work at sites like https://bitcoinwisdom.io/markets/bitfinex/btcusd and I have a minor use for them. So I installed the libwebsockets deb and grabbed a tutorial off the web. Except the example in the tutorial won't compile, looks like the wrong library or something. So I removed the deb and installed from https://libwebsockets.org/ which has plenty of example programs.

But I'm wondering how standardized websockets are. My application will have a server and a client, can I tell users to just install the deb or am I dooming them to install from source if I use the libwebsockets.org version? I want a server written in C which both versions apparently have. The libwebsockets.org one has better documentation I think. But websockets can be written in many languages. https://en.wikipedia.org/wiki/WebSocket and https://en.wikipedia.org/wiki/Compariso ... mentations
ab1jx
 
Posts: 61
Joined: 2016-01-23 21:28
Location: Heath, MA, USA

Re: Anybody use web sockets?

Postby ab1jx » 2021-02-16 02:33

They're maybe drop-in replacements. Cmake build of the websockets.org one left me with no uninstall, but I edited install_manifest into a script that did rm -f on each file. Ran it, then reinstalled libwebsockets from the deb.

One at least of the demo programs from the websockets.org set still runs (against the deb's libraries?). Better yet the source still compiles. Now that I've got it out I might reinstall it with a PREFIX of my choosing but I didn't think to do that the first time.

OK, not interchangeable.

CMake Error at /data/src/websockets/libwebsockets/build/LwsCheckRequirements.cmake:50 (message):
This project requires lws must have been configured with LWS_WITH_CGI
Call Stack (most recent call first):
CMakeLists.txt:13 (require_lws_config)
-- Configuring incomplete, errors occurred!
See also "/data/src/websockets/libwebsockets/minimal-examples/http-server/minimal-http-server-cgi/CMakeFiles/CMakeOutput.log".
See also "/data/src/websockets/libwebsockets/minimal-examples/http-server/minimal-http-server-cgi/CMakeFiles/CMakeError.log".

I'm trying to build a demo cgi server here.

OK, my application is https://sourceforge.net/projects/cgi-jukebox/ which works but I want to add a volume control. Probably by running amixer remotely and parsing the output. I already use conventional CGI but getting 2-way communication with it is a pain since it's not really meant for it. I think web sockets would work much better, maybe even replacing Apache and its CGI, I don't know yet.

With conventional CGI everything sends some response, even if you don't want one. My jukebox program jumps through hoops to end up back on the page it started on after it plays a song. Websockets would probably avoid that. I could just send the file number of the song to play to the server on a click. I could bring up a volume dialog on a click. I could keep the part that scans directories of MP3 files and makes web pages, the URLs would just change.
ab1jx
 
Posts: 61
Joined: 2016-01-23 21:28
Location: Heath, MA, USA

Re: Anybody use web sockets?

Postby ab1jx » 2021-02-19 02:07

Still working on it. In the tarball I grabbed is a directory called minimal-examples. That, in a tree structure has maybe 100 little C examples for different situations. You don't get that if you install the deb. I set the CMAKE_INSTALL_PREFIX=/usr/local/ws2 to have them not interfere with the deb stuff, ran cmake with that define, make, make install and the files went where I wanted them. But minimal-examples ls only in the tarball, it doesn't get installed. When you extract it's in the dir where you put it but it stops there.

This file will compile into a minimal HTTP server (with the deb's libs):

Code: Select all
/*
 * lws-minimal-http-server
 *
 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
 *
 * This file is made available under the Creative Commons CC0 1.0
 * Universal Public Domain Dedication.
 *
 * This demonstrates the most minimal http server you can make with lws.
 *
 * To keep it simple, it serves stuff from the subdirectory
 * "./mount-origin" of the directory it was started in.
 * You can change that by changing mount.origin below.
 */

#include <libwebsockets.h>
#include <string.h>
#include <signal.h>

static int interrupted;

static const struct lws_http_mount mount = {
   /* .mount_next */      NULL,      /* linked-list "next" */
   /* .mountpoint */      "/",      /* mountpoint URL */
   /* .origin */         "./mount-origin", /* serve from dir */
   /* .def */         "index.html",   /* default filename */
   /* .protocol */         NULL,
   /* .cgienv */         NULL,
   /* .extra_mimetypes */      NULL,
   /* .interpret */      NULL,
   /* .cgi_timeout */      0,
   /* .cache_max_age */      0,
   /* .auth_mask */      0,
   /* .cache_reusable */      0,
   /* .cache_revalidate */      0,
   /* .cache_intermediaries */   0,
   /* .origin_protocol */      LWSMPRO_FILE,   /* files in a dir */
   /* .mountpoint_len */      1,      /* char count */
   /* .basic_auth_login_file */   NULL,
};

void sigint_handler(int sig)
{
   interrupted = 1;
}

int main(int argc, const char **argv)
{
   struct lws_context_creation_info info;
   struct lws_context *context;
   const char *p;
   int n = 0, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE
         /* for LLL_ verbosity above NOTICE to be built into lws,
          * lws must have been configured and built with
          * -DCMAKE_BUILD_TYPE=DEBUG instead of =RELEASE */
         /* | LLL_INFO */ /* | LLL_PARSER */ /* | LLL_HEADER */
         /* | LLL_EXT */ /* | LLL_CLIENT */ /* | LLL_LATENCY */
         /* | LLL_DEBUG */;

   signal(SIGINT, sigint_handler);

   if ((p = lws_cmdline_option(argc, argv, "-d")))
      logs = atoi(p);

   lws_set_log_level(logs, NULL);
   lwsl_user("LWS minimal http server | visit http://localhost:7681\n");

   memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
   info.port = 7681;
   info.mounts = &mount;
   info.error_document_404 = "/404.html";
   info.options =
      LWS_SERVER_OPTION_HTTP_HEADERS_SECURITY_BEST_PRACTICES_ENFORCE;

   if (lws_cmdline_option(argc, argv, "--h2-prior-knowledge"))
      info.options |= LWS_SERVER_OPTION_H2_PRIOR_KNOWLEDGE;

   context = lws_create_context(&info);
   if (!context) {
      lwsl_err("lws init failed\n");
      return 1;
   }

   while (n >= 0 && !interrupted)
      n = lws_service(context, 0);

   lws_context_destroy(context);

   return 0;
}

And it actually works. I guess I'll start there, and just avoid using anything that doesn't work with the deb. You have to use cmake which sets up stuff I didn't mention here so don't try this at home. The directory has a CMakeLists.txt file that gets processed.

There are these categories of examples, which are pretty vital: (| should probably be a tab here)
Code: Select all
client-server|Minimal examples providing client and server connections simultaneously
crypto|Minimal examples related to using lws crypto apis
dbus-server|Minimal examples showing how to integrate DBUS into lws event loop
http-client|Minimal examples providing an http client
http-server|Minimal examples providing an http server
raw|Minimal examples related to adopting raw file or socket descriptors into the event loop
secure-streams|Minimal examples related to the Secure Streams client api
ws-client|Minimal examples providing a ws client
ws-server|Minimal examples providing a ws server (and an http server)


Maybe there should be a libwebsockets-doc deb which includes minimal-examples? I'm not sure it's 100% compatible.
ab1jx
 
Posts: 61
Joined: 2016-01-23 21:28
Location: Heath, MA, USA

Re: Anybody use web sockets?

Postby ab1jx » 2021-02-20 11:55

OK, this is probably significant:
Code: Select all
locate libwebsockets.so
/data/src/websockets/libwebsockets/build/lib/libwebsockets.so
/data/src/websockets/libwebsockets/build/lib/libwebsockets.so.17
/usr/lib/aarch64-linux-gnu/libwebsockets.so
/usr/lib/aarch64-linux-gnu/libwebsockets.so.16
/usr/local/ws2/lib/libwebsockets.so
/usr/local/ws2/lib/libwebsockets.so.17


The libwebsockets.org and the deb may be the same, but the deb is a version older. And this is in Bullseye. So I'll get rid of both versions and reinstall the libwebsockets.org one in its default location. I was seeing some really screwy stuff going on, like I'd change websockets to use a different html directory and suddenly the basic demo program wouldn't compile, I'd comment out something that was coming up undefined and suddenly get a segfault. Valgrind told me it was trying to write outside its address space.
ab1jx
 
Posts: 61
Joined: 2016-01-23 21:28
Location: Heath, MA, USA

Re: Anybody use web sockets?

Postby ab1jx » 2021-02-20 21:26

Here's a slick demo where you drive a racecar by tilting your phone https://websocket.org/demos/racer/index.html. The serious front page is https://libwebsockets.org/ The more glitzy front page is at https://websocket.org/ There's a book advertised at https://websocket.org/book.html which they seem to be selling on Amazon in print or ebook form.

There's a mailing list https://libwebsockets.org/mailman/listi ... websockets but there doesn't seem to be an official forum https://duckduckgo.com/?t=ffnt&q=websoc ... rum&ia=web Mailing list archives at https://libwebsockets.org/pipermail/libwebsockets/ it's Pipermail, you can download gzipped monthly batches. Kazing documentation at https://kaazing.com/doc/5.0/index.html This describes the minimal-examples which you don't get by installing the deb: https://libwebsockets.org/git/libwebsoc ... l-examples MIT license but there also is a company and they're hiring. Websockets is part of HTML5 so it's an evolving standard.

Online test server https://libwebsockets.org/testserver/
Websockets at w3.org: https://www.w3.org/TR/websockets/
ab1jx
 
Posts: 61
Joined: 2016-01-23 21:28
Location: Heath, MA, USA


Return to Programming

Who is online

Users browsing this forum: No registered users and 7 guests

fashionable