[Libwebsockets] getting references to websocket private data
andy at warmcat.com
Thu Nov 6 01:00:21 CET 2014
On 6 November 2014 00:25:30 GMT+08:00, Sagaert Johan <sagaert.johan at skynet.be> wrote:
>Just getting started using libwebsockets.
>I am using a global var and then use
>to request sending data back to the client.
>So this data gets sent in the callback function (case
>LWS_CALLBACK_SERVER_WRITEABLE) with the libwebsocket_write api.
>This works, but I have no control over to wich remote client I can sent
>data.(every one receives the same data)
Well, you do have control in the sense that everybody's WRITEABLE callback will fire individually, when it is possible to write to that connection.
So in the callback instance you could decide if you actually want to send what you have to that guy.
But I agree with a lot of connections that approach is wasteful.
If your application only wants to send one thing to one connection, it's better if you were able to ask just for the WRITEABLE callback for that wsi (and bind the data to his user struct, not a global, so you can have data intended for multiple connections in flight at the same time safely).
>Is there some way I can get a reference to the private data structure
>associated with a websocket, so I can fill in the data to be
>sent in this private struct that then would be sent in the callback
>function. (as in the test-echo example)
>Do I need to create my own data structures/lists of connected clients
>and populate this in the callback function?
Yeah. You can hook these callbacks to know the lifecycle of every wsi
* LWS_CALLBACK_WSI_CREATE: outermost (earliest) wsi create notification
* LWS_CALLBACK_WSI_DESTROY: outermost (latest) wsi destroy notification
LWS_CALLBACK_ESTABLISHED to know the user pointer
LWS_CALLBACK_CLOSED to know he can't be used any more
>Next question is, how do I get the clients IP ?
You can get the socket descriptor for a wsi directly using
then call whatever your OS provides to get the peer IP from a socket fd.
>lws_hdr_copy(wsi, buf, sizeof buf, WSI_TOKEN_KEY); is nog giving me the
>correct ip, it just gives me the site hosting the html/js
Yeah those are only real headers passed from the client, it's not like PHP where we added some fake headers with the metadata. You couldn't trust an IP that was sent by the client as data, you have to go determine it yourself via the actual connection's socket fd.
>I want to know this ip so I can create a blacklist of ip's banned for
>some time when not they are not
>sending the correct credentials in time.
It should be enough and check your blacklist at
>Thanks in advance.
>Libwebsockets mailing list
>Libwebsockets at ml.libwebsockets.org
More information about the Libwebsockets