diff --git a/examples/websocket/WebSockCallbacks.c b/examples/websocket/WebSockCallbacks.c index 23281a4f..ebcbda73 100644 --- a/examples/websocket/WebSockCallbacks.c +++ b/examples/websocket/WebSockCallbacks.c @@ -138,15 +138,14 @@ void connection_close_handler(struct mg_connection *conn) { } -static int runLoop = 0; - static void * eventMain(void * arg) { char msg[256]; struct mg_context *ctx = (struct mg_context *)arg; + tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx); - runLoop = 1; - while (runLoop) { + ws_ctx->runLoop = 1; + while (ws_ctx->runLoop) { time_t t = time(0); struct tm * timestr = localtime(&t); sprintf(msg,"title %s",asctime(timestr)); @@ -174,14 +173,15 @@ void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_ void websock_init_lib(struct mg_context *ctx) { tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx); - memset(ws_ctx->socketList,0,sizeof(ws_ctx->socketList)); + memset(ws_ctx,0,sizeof(*ws_ctx)); /* todo: use mg_start_thread_id instead of mg_start_thread */ mg_start_thread(eventMain, ctx); } void websock_exit_lib(struct mg_context *ctx) { - runLoop = 0; + tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx); + ws_ctx->runLoop = 0; /* todo: wait for the thread instead of a timeout */ mg_sleep(2000); } diff --git a/examples/websocket/WebSockCallbacks.h b/examples/websocket/WebSockCallbacks.h index ad968523..d7be795c 100644 --- a/examples/websocket/WebSockCallbacks.h +++ b/examples/websocket/WebSockCallbacks.h @@ -16,6 +16,7 @@ typedef struct tWebSockInfo { #define MAX_NUM_OF_WEBSOCKS (256) typedef struct tWebSockContext { + int runLoop; void * thread_id; tWebSockInfo *socketList[MAX_NUM_OF_WEBSOCKS]; } tWebSockContext;