This function creates the listening socket and takes care of all initialization in one step.After initialization, it forks a thread that will sits in a service loop and returns to the caller. The actual service actions are performed by user code in a per-protocol callback from the appropriate one selected by the client from the list in protocols.
The protocol callback functions are called for a handful of events including http requests coming in, websocket connections becoming established, and data arriving; it's also called periodically to allow async transmission.
HTTP requests are sent always to the FIRST protocol in protocol, since at that time websocket protocol has not been negotiated. Other protocols after the first one never see any HTTP callack activity.
The server created is a simple http server by default; part of the websocket standard is upgrading this http connection to a websocket one.
This allows the same server to provide files like scripts and favicon / images or whatever over http and dynamic data over websockets all in one place; they're all handled in the user callback.
This is useful to get the protocol to broadcast back to from inside the callback.
This function allows bulk sending of a packet to every connection using the given protocol. It does not send the data directly; instead it calls the callback with a reason type of LWS_CALLBACK_BROADCAST. If the callback wants to actually send the data for that connection, the callback itself should call libwebsocket_write.libwebsockets_broadcast can be called from another fork context without having to take any care about data visibility between the processes, it'll "just work".
This function provides the way to issue data back to the client for both http and websocket protocols.In the case of sending using websocket protocol, be sure to allocate valid storage before and after buf as explained above. This scheme allows maximum efficiency of sending data and protocol in a single packet while not burdening the user code with any protocol knowledge.
This function is intended to be called from the callback in response to http requests from the client. It allows the callback to issue local files down the http link in a single step.
This callback is the way the user controls what is served. All the protocol detail is hidden and handled by the library.For each connection / session there is user data allocated that is pointed to by "user". You set the size of this user data area when the library is initialized with libwebsocket_create_server.
You get an opportunity to initialize user data when called back with LWS_CALLBACK_ESTABLISHED reason.
after successful websocket handshake
when the websocket session ends
signal to send to client (you would use libwebsocket_write taking care about the special buffer requirements
data has appeared for the server, it can be found at *in and is len bytes long
an http request has come from a client that is not asking to upgrade the connection to a websocket one. This is a chance to serve http content, for example, to send a script to the client which will then open the websockets connection. in points to the URI path requested and libwebsockets_serve_http_file makes it very simple to send back a file to the client.
This structure represents one protocol supported by the server. An array of these structures is passed to libwebsocket_create_server allows as many protocols as you like to be handled by one server.