[Libwebsockets] What is the proper way to send back websouckets response NOT to all connected clients

Meir Yanovich meiry242 at gmail.com
Wed May 25 12:54:32 CEST 2016


Hello all
i want to be able to send back response only to selected connected clients
and not as
shown in the example using :
lws_callback_on_writable_all_protocol.
i searched the mailing list and found this answer :
https://libwebsockets.org/pipermail/libwebsockets/2016-January/002177.html

As suggested i need to keep list of wsi amd user id's
I just didn't understand why i need to use lws_callback_on_writable()
As you can see i will invoke function to response to selected users
In the LWS_CALLBACK_SERVER_WRITEABLE case

Now what i have in my web socket handler/callback function :
where do i need to use lws_callback_on_writable() ?

switch (reason) { case LWS_CALLBACK_ESTABLISHED:
pss->recive_all_from_client = 0; break; case LWS_CALLBACK_SERVER_WRITEABLE:
if (pss->recive_all_from_client == 1) { root = cJSON_CreateObject();
cJSON_AddItemToObject(root, "text", cJSON_CreateString(pss->text_string));
cJSON_AddItemToObject(root, "id", cJSON_CreateString(pss->id_string));
cJSON_AddItemToObject(root, "msg", cJSON_CreateString("Thanks!"));
resp_json = cJSON_Print(root); n = sprintf((char *)pout, "%s", resp_json);
//m = lws_write(wsi, out, sizeof(out), LWS_WRITE_TEXT); if (m < n) {
lwsl_err("ERROR %d writing to di socket\n", sizeof(out)); return -1; } /*
// Here i will keep the wsi and the user id in some vector or list */
//Invoke response to selected users ... } break; case LWS_CALLBACK_RECEIVE:
if (len < 1) { break; } lwsl_notice("Request from server: %s \n", (const
char *)in); cJSON * user_root = cJSON_Parse((const char *)in); cJSON *
user_type = cJSON_GetObjectItem(user_root, "type"); cJSON * user_text =
cJSON_GetObjectItem(user_root, "text"); cJSON * user_id =
cJSON_GetObjectItem(user_root, "id"); cJSON * user_date =
cJSON_GetObjectItem(user_root, "date"); strcpy(pss->type_string,
user_type->valuestring); strcpy(pss->text_string, user_text->valuestring);
strcpy(pss->id_string, user_id->valuestring); strcpy(pss->date_string,
user_date->valuestring); pss->recive_all_from_client = 1; break; case
LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION:
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://libwebsockets.org/pipermail/libwebsockets/attachments/20160525/ef197d76/attachment.html>


More information about the Libwebsockets mailing list