1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-07-31 08:24:23 +03:00

Update websocket example - store websocket clients per server context instead of using static variables

This commit is contained in:
bel
2014-08-11 00:22:07 +02:00
parent 265d2309a1
commit 624aed908d
3 changed files with 35 additions and 25 deletions

View File

@ -14,25 +14,15 @@
#endif
typedef struct tWebSockInfo {
int webSockState;
unsigned long initId;
struct mg_connection *conn;
} tWebSockInfo;
#define MAX_NUM_OF_WEBSOCKS (256)
static tWebSockInfo *socketList[MAX_NUM_OF_WEBSOCKS];
static void send_to_all_websockets(struct mg_context *ctx, const char * data, int data_len) {
int i;
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
mg_lock_context(ctx);
for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
if (socketList[i] && (socketList[i]->webSockState==2)) {
mg_websocket_write(socketList[i]->conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
if (ws_ctx->socketList[i] && (ws_ctx->socketList[i]->webSockState==2)) {
mg_websocket_write(ws_ctx->socketList[i]->conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
}
}
mg_unlock_context(ctx);
@ -44,6 +34,7 @@ void websocket_ready_handler(struct mg_connection *conn) {
int i;
struct mg_request_info * rq = mg_get_request_info(conn);
struct mg_context * ctx = mg_get_context(conn);
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
tWebSockInfo * wsock = malloc(sizeof(tWebSockInfo));
assert(wsock);
wsock->webSockState = 0;
@ -51,8 +42,8 @@ void websocket_ready_handler(struct mg_connection *conn) {
mg_lock_context(ctx);
for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
if (0==socketList[i]) {
socketList[i] = wsock;
if (0==ws_ctx->socketList[i]) {
ws_ctx->socketList[i] = wsock;
wsock->conn = conn;
wsock->webSockState = 1;
break;
@ -63,14 +54,15 @@ void websocket_ready_handler(struct mg_connection *conn) {
}
static void websocket_done(tWebSockInfo * wsock) {
static void websocket_done(tWebSockContext *ws_ctx, tWebSockInfo * wsock) {
int i;
if (wsock) {
wsock->webSockState = 99;
for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
if (wsock==socketList[i]) {
socketList[i] = 0;
if (wsock==ws_ctx->socketList[i]) {
ws_ctx->socketList[i] = 0;
break;
}
}
@ -83,14 +75,15 @@ static void websocket_done(tWebSockInfo * wsock) {
int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len) {
struct mg_request_info * rq = mg_get_request_info(conn);
struct mg_context * ctx = mg_get_context(conn);
tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
struct mg_context * ctx = mg_get_context(conn);
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
char msg[128];
mg_lock_context(ctx);
if (flags==136) {
// close websock
websocket_done(wsock);
websocket_done(ws_ctx, wsock);
rq->conn_data = 0;
mg_unlock_context(ctx);
return 1;
@ -134,11 +127,12 @@ int websocket_data_handler(struct mg_connection *conn, int flags, char *data, si
void connection_close_handler(struct mg_connection *conn) {
struct mg_request_info * rq = mg_get_request_info(conn);
struct mg_context * ctx = mg_get_context(conn);
tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
struct mg_context * ctx = mg_get_context(conn);
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
mg_lock_context(ctx);
websocket_done(wsock);
websocket_done(ws_ctx, wsock);
rq->conn_data = 0;
mg_unlock_context(ctx);
}
@ -179,8 +173,9 @@ void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_
void websock_init_lib(struct mg_context *ctx) {
/* todo: use variable in the ctx instead of static ones */
memset(socketList,0,sizeof(socketList));
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
memset(ws_ctx->socketList,0,sizeof(ws_ctx->socketList));
/* todo: use mg_start_thread_id instead of mg_start_thread */
mg_start_thread(eventMain, ctx);
}

View File

@ -8,6 +8,19 @@
extern "C" {
#endif
typedef struct tWebSockInfo {
int webSockState;
unsigned long initId;
struct mg_connection *conn;
} tWebSockInfo;
#define MAX_NUM_OF_WEBSOCKS (256)
typedef struct tWebSockContext {
void * thread_id;
tWebSockInfo *socketList[MAX_NUM_OF_WEBSOCKS];
} tWebSockContext;
void websock_init_lib(struct mg_context *ctx);
void websock_exit_lib(struct mg_context *ctx);

View File

@ -9,6 +9,7 @@ int main(void)
{
struct mg_context *ctx = 0;
struct mg_callbacks callback_funcs = {0};
tWebSockContext ws_ctx;
char inbuf[4];
const char *server_options[] = {
@ -27,7 +28,8 @@ int main(void)
callback_funcs.websocket_ready = websocket_ready_handler;
callback_funcs.websocket_data = websocket_data_handler;
callback_funcs.connection_close = connection_close_handler;
ctx = mg_start(&callback_funcs, NULL, server_options);
ctx = mg_start(&callback_funcs, &ws_ctx, server_options);
printf("Connect to localhost:%s/websock.htm\n", mg_get_option(ctx, "listening_ports"));
puts("Enter an (ASCII) character or * to exit:");