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:
@ -14,25 +14,15 @@
|
|||||||
#endif
|
#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) {
|
static void send_to_all_websockets(struct mg_context *ctx, const char * data, int data_len) {
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
|
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
|
||||||
|
|
||||||
mg_lock_context(ctx);
|
mg_lock_context(ctx);
|
||||||
for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
|
for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
|
||||||
if (socketList[i] && (socketList[i]->webSockState==2)) {
|
if (ws_ctx->socketList[i] && (ws_ctx->socketList[i]->webSockState==2)) {
|
||||||
mg_websocket_write(socketList[i]->conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
|
mg_websocket_write(ws_ctx->socketList[i]->conn, WEBSOCKET_OPCODE_TEXT, data, data_len);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mg_unlock_context(ctx);
|
mg_unlock_context(ctx);
|
||||||
@ -44,6 +34,7 @@ void websocket_ready_handler(struct mg_connection *conn) {
|
|||||||
int i;
|
int i;
|
||||||
struct mg_request_info * rq = mg_get_request_info(conn);
|
struct mg_request_info * rq = mg_get_request_info(conn);
|
||||||
struct mg_context * ctx = mg_get_context(conn);
|
struct mg_context * ctx = mg_get_context(conn);
|
||||||
|
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
|
||||||
tWebSockInfo * wsock = malloc(sizeof(tWebSockInfo));
|
tWebSockInfo * wsock = malloc(sizeof(tWebSockInfo));
|
||||||
assert(wsock);
|
assert(wsock);
|
||||||
wsock->webSockState = 0;
|
wsock->webSockState = 0;
|
||||||
@ -51,8 +42,8 @@ void websocket_ready_handler(struct mg_connection *conn) {
|
|||||||
|
|
||||||
mg_lock_context(ctx);
|
mg_lock_context(ctx);
|
||||||
for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
|
for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
|
||||||
if (0==socketList[i]) {
|
if (0==ws_ctx->socketList[i]) {
|
||||||
socketList[i] = wsock;
|
ws_ctx->socketList[i] = wsock;
|
||||||
wsock->conn = conn;
|
wsock->conn = conn;
|
||||||
wsock->webSockState = 1;
|
wsock->webSockState = 1;
|
||||||
break;
|
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;
|
int i;
|
||||||
|
|
||||||
if (wsock) {
|
if (wsock) {
|
||||||
wsock->webSockState = 99;
|
wsock->webSockState = 99;
|
||||||
for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
|
for (i=0;i<MAX_NUM_OF_WEBSOCKS;i++) {
|
||||||
if (wsock==socketList[i]) {
|
if (wsock==ws_ctx->socketList[i]) {
|
||||||
socketList[i] = 0;
|
ws_ctx->socketList[i] = 0;
|
||||||
break;
|
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) {
|
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_request_info * rq = mg_get_request_info(conn);
|
||||||
struct mg_context * ctx = mg_get_context(conn);
|
|
||||||
tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
|
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];
|
char msg[128];
|
||||||
|
|
||||||
mg_lock_context(ctx);
|
mg_lock_context(ctx);
|
||||||
if (flags==136) {
|
if (flags==136) {
|
||||||
// close websock
|
// close websock
|
||||||
websocket_done(wsock);
|
websocket_done(ws_ctx, wsock);
|
||||||
rq->conn_data = 0;
|
rq->conn_data = 0;
|
||||||
mg_unlock_context(ctx);
|
mg_unlock_context(ctx);
|
||||||
return 1;
|
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) {
|
void connection_close_handler(struct mg_connection *conn) {
|
||||||
|
|
||||||
struct mg_request_info * rq = mg_get_request_info(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;
|
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);
|
mg_lock_context(ctx);
|
||||||
websocket_done(wsock);
|
websocket_done(ws_ctx, wsock);
|
||||||
rq->conn_data = 0;
|
rq->conn_data = 0;
|
||||||
mg_unlock_context(ctx);
|
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) {
|
void websock_init_lib(struct mg_context *ctx) {
|
||||||
|
|
||||||
/* todo: use variable in the ctx instead of static ones */
|
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
|
||||||
memset(socketList,0,sizeof(socketList));
|
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);
|
mg_start_thread(eventMain, ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,19 @@
|
|||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#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_init_lib(struct mg_context *ctx);
|
||||||
void websock_exit_lib(struct mg_context *ctx);
|
void websock_exit_lib(struct mg_context *ctx);
|
||||||
|
|
||||||
|
@ -9,6 +9,7 @@ int main(void)
|
|||||||
{
|
{
|
||||||
struct mg_context *ctx = 0;
|
struct mg_context *ctx = 0;
|
||||||
struct mg_callbacks callback_funcs = {0};
|
struct mg_callbacks callback_funcs = {0};
|
||||||
|
tWebSockContext ws_ctx;
|
||||||
char inbuf[4];
|
char inbuf[4];
|
||||||
|
|
||||||
const char *server_options[] = {
|
const char *server_options[] = {
|
||||||
@ -27,7 +28,8 @@ int main(void)
|
|||||||
callback_funcs.websocket_ready = websocket_ready_handler;
|
callback_funcs.websocket_ready = websocket_ready_handler;
|
||||||
callback_funcs.websocket_data = websocket_data_handler;
|
callback_funcs.websocket_data = websocket_data_handler;
|
||||||
callback_funcs.connection_close = connection_close_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"));
|
printf("Connect to localhost:%s/websock.htm\n", mg_get_option(ctx, "listening_ports"));
|
||||||
|
|
||||||
puts("Enter an (ASCII) character or * to exit:");
|
puts("Enter an (ASCII) character or * to exit:");
|
||||||
|
Reference in New Issue
Block a user