[Libwebsockets] How does lws send javascript code?!

Andy Green andy at warmcat.com
Sat Jul 27 11:37:08 CEST 2019



On July 27, 2019 10:10:11 AM GMT+01:00, Alexander Zvyagin <zvyagin.alexander at gmail.com> wrote:
>Hi!
>
>I would like to understand how LWS sends javascript code to a client.
>
>In my application the "client" code consists of two files:
>
>  index.html  (size=400 Bytes)
>  main.js (size=4 MBytes)
>
>And the "index.html" has the line:
>
>  <script type="text/javascript" src="main.js"></script></body>
>
>The "server" code defines lws_http_mount structure with attributes:
>
>  .mountpoint="/"
>  .mountpoint_len=1
>  .origin_protocol=LWSMPRO_FILE
>  .def="index.html"
>.origin="some/path"  (a directory with "index.html" and "main.js"
>files)
>
>My _guess_ is that the logic is:
>
>- a client connects to the server with mountpoint="/" OR

Mountpoint is in the url space of the server.  So you'd normally map / to an origin directory that contains all your static files.

>mountpoint="/index.html"
>- the server sends back the "index.html" file
>- the client (a web browser) gets "index.html", sees src="main.js" and
>asks the server to send the file content
>- the server gets the request on "main.js" files and because it is
>allowed to serve the files (origin_protocol=LWSMPRO_FILE) and the
>directory (origin="some/path") contains that file, it reads the file
>and sends it back to the client.
>- finally the client has both "index.html" and "main.js" files (the
>latter will initialize the websocket connection with the server)

Yes that's right.  It's the same for images referred to in the html.

I'm guessing since you mention 4MB.js, this is something you don't want to have the clIent transfer every time. lws supports ETAGs and http clientside cache... gitohashi shows how to se the mount, although that uses lwsws / JSON to do it, it's setting members in the mount struct.

https://warmcat.com/git/gitohashi/tree/etc-gitohashi/conf.d/localhost#n34-37

This way it's transferred once and for whatever max-age you sent, only the response code saying your cached version can be used is sent by the server.

-Andy

>Am I right?
>
>Thanks a lot in advance!
>Alexander.
>_______________________________________________
>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org
>https://libwebsockets.org/mailman/listinfo/libwebsockets


More information about the Libwebsockets mailing list