int main(int argc, char **argv) { int port = 8080; unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1024 + LWS_SEND_BUFFER_POST_PADDING]; struct libwebsocket_context *context = libwebsocket_create_context( port, "", protocols, libwebsocket_internal_extensions, nullptr, nullptr, -1, -1, 0); if (context == NULL) { fprintf(stderr, "libwebsocket init failed\n"); return -1; } buf[LWS_SEND_BUFFER_PRE_PADDING] = 'x'; int n = libwebsockets_fork_service_loop(context); if (n < 0) { fprintf(stderr, "Unable to fork service loop %d\n", n); return 1; } while(true) { sleep(1); libwebsockets_broadcast(&protocols[PROTOCOL_NRT_WS], &buf[LWS_SEND_BUFFER_PRE_PADDING], 1); } libwebsocket_context_destroy(context); return 0; }
void EchoDataWebSocket::broadcastMsgToClients(char* msg, int nLen) { libwebsocket_protocols* protoc = Webserver::getImplementedProtocols(); echoBuffer[LWS_SEND_BUFFER_PRE_PADDING] = 'x'; memcpy((char*)&broadcastEchoMsg[LWS_SEND_BUFFER_PRE_PADDING], msg, nLen); broadcastEchoLength = nLen; libwebsockets_broadcast(&protoc[PROTOCOL_DUMB_INCREMENT], &echoBuffer[LWS_SEND_BUFFER_PRE_PADDING], 1); //PROTOCOL_LWS_MIRROR = 2 #ifdef DEBUG_WEBSERVER printf("Broadcast ECHO %d bytes\n", nLen); #endif }
//-------------------------------------------------------------- void Protocol::broadcast(const std::string& message){ std::string buf(LWS_SEND_BUFFER_PRE_PADDING+message.size()+LWS_SEND_BUFFER_POST_PADDING, 0); unsigned char *p = (unsigned char*)&buf[LWS_SEND_BUFFER_PRE_PADDING]; if (reactor != NULL) { memcpy(p, message.c_str(), message.size()); int n = libwebsockets_broadcast(&reactor->lws_protocols[idx], p, message.size()); if (n < 0) fprintf(stderr, "ERROR writing to socket"); } }
int main(int argc, char **argv) { int n = 0; const char *cert_path = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.pem"; const char *key_path = LOCAL_RESOURCE_PATH"/libwebsockets-test-server.key.pem"; unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1024 + LWS_SEND_BUFFER_POST_PADDING]; int port = 7681; int use_ssl = 0; struct libwebsocket_context *context; int opts = 0; char interface_name[128] = ""; const char *interface = NULL; #ifdef LWS_NO_FORK unsigned int oldus = 0; #endif fprintf(stderr, "libwebsockets test server\n" "(C) Copyright 2010-2011 Andy Green <*****@*****.**> " "licensed under LGPL2.1\n"); while (n >= 0) { n = getopt_long(argc, argv, "ci:khsp:", options, NULL); if (n < 0) continue; switch (n) { case 's': use_ssl = 1; break; case 'k': opts = LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK; break; case 'p': port = atoi(optarg); break; case 'i': strncpy(interface_name, optarg, sizeof interface_name); interface_name[(sizeof interface_name) - 1] = '\0'; interface = interface_name; break; case 'c': close_testing = 1; fprintf(stderr, " Close testing mode -- closes on " "client after 50 dumb increments" "and suppresses lws_mirror spam\n"); break; case 'h': fprintf(stderr, "Usage: test-server " "[--port=<p>] [--ssl]\n"); exit(1); } } if (!use_ssl) cert_path = key_path = NULL; context = libwebsocket_create_context(port, interface, protocols, libwebsocket_internal_extensions, cert_path, key_path, -1, -1, opts); if (context == NULL) { fprintf(stderr, "libwebsocket init failed\n"); return -1; } buf[LWS_SEND_BUFFER_PRE_PADDING] = 'x'; #ifdef LWS_NO_FORK /* * This example shows how to work with no forked service loop */ fprintf(stderr, " Using no-fork service loop\n"); n = 0; while (n >= 0) { struct timeval tv; gettimeofday(&tv, NULL); /* * This broadcasts to all dumb-increment-protocol connections * at 20Hz. * * We're just sending a character 'x', in these examples the * callbacks send their own per-connection content. * * You have to send something with nonzero length to get the * callback actions delivered. * * We take care of pre-and-post padding allocation. */ if (((unsigned int)tv.tv_usec - oldus) > 50000) { libwebsockets_broadcast( &protocols[PROTOCOL_DUMB_INCREMENT], &buf[LWS_SEND_BUFFER_PRE_PADDING], 1); oldus = tv.tv_usec; } /* * This example server does not fork or create a thread for * websocket service, it all runs in this single loop. So, * we have to give the websockets an opportunity to service * "manually". * * If no socket is needing service, the call below returns * immediately and quickly. Negative return means we are * in process of closing */ n = libwebsocket_service(context, 50); } #else /* * This example shows how to work with the forked websocket service loop */ fprintf(stderr, " Using forked service loop\n"); /* * This forks the websocket service action into a subprocess so we * don't have to take care about it. */ n = libwebsockets_fork_service_loop(context); if (n < 0) { fprintf(stderr, "Unable to fork service loop %d\n", n); return 1; } while (1) { usleep(50000); /* * This broadcasts to all dumb-increment-protocol connections * at 20Hz. * * We're just sending a character 'x', in these examples the * callbacks send their own per-connection content. * * You have to send something with nonzero length to get the * callback actions delivered. * * We take care of pre-and-post padding allocation. */ libwebsockets_broadcast(&protocols[PROTOCOL_DUMB_INCREMENT], &buf[LWS_SEND_BUFFER_PRE_PADDING], 1); } #endif libwebsocket_context_destroy(context); return 0; }
DWORD STDCALL MainWebSocketThread(LPVOID lpUnused) { struct libwebsocket_context *context; const char *interface = NULL; unsigned char buf[LWS_SEND_BUFFER_PRE_PADDING + 1024 + LWS_SEND_BUFFER_POST_PADDING]; context = libwebsocket_create_context(port, interface, protocols, libwebsocket_internal_extensions, NULL, NULL, NULL, -1, -1, 0, NULL); unsigned int oldus = 0; int n = 0; buf[LWS_SEND_BUFFER_PRE_PADDING] = 'x'; //json_set_alloc_funcs(malloc, free); while (n >= 0 && running) { struct timeval tv; gettimeofday(&tv, NULL); /* * Send out all recieved update broadcasts. */ json_t* message = NULL; while((message = triggerHandler->popUpdate()) != NULL) { char *messageText = json_dumps(message, 0); if(messageText != NULL) { int sendLength = strlen(messageText); /* copy json text into memory buffer properly framed for libwebsockets */ char* messageBuf = (char*) malloc(LWS_SEND_BUFFER_PRE_PADDING + sendLength + LWS_SEND_BUFFER_POST_PADDING); memcpy(messageBuf + LWS_SEND_BUFFER_PRE_PADDING, messageText, sendLength); n = libwebsockets_broadcast(&protocols[PROTOCOL_OBS_API], (unsigned char *) messageBuf + LWS_SEND_BUFFER_PRE_PADDING, sendLength); if (n < 0) { fprintf(stderr, "ERROR writing to socket"); } free(messageBuf); free((void *)messageText); } json_decref(message); } /* * This example server does not fork or create a thread for * websocket service, it all runs in this single loop. So, * we have to give the websockets an opportunity to service * "manually". * * If no socket is needing service, the call below returns * immediately and quickly. Negative return means we are * in process of closing */ n = libwebsocket_service(context, 50); } libwebsocket_context_destroy(context); return 0; }