mirror of
https://github.com/lammertb/libhttp.git
synced 2025-07-29 21:01:13 +03:00
Handler for websocket connection (Step 3/?)
Add an interface to register handler for websocket connections. See enhancement #30 and question #101
This commit is contained in:
@ -121,7 +121,8 @@ int WebSocketStartHandler(struct mg_connection *conn, void *cbdata)
|
|||||||
|
|
||||||
|
|
||||||
#ifdef USE_WEBSOCKET
|
#ifdef USE_WEBSOCKET
|
||||||
static struct mg_connection * ws_client;
|
#define MAX_WS_CLIENTS 1024
|
||||||
|
static struct mg_connection * ws_clients[MAX_WS_CLIENTS];
|
||||||
static unsigned long cnt;
|
static unsigned long cnt;
|
||||||
|
|
||||||
int WebSocketConnectHandler(const struct mg_connection * conn, void *cbdata)
|
int WebSocketConnectHandler(const struct mg_connection * conn, void *cbdata)
|
||||||
@ -134,13 +135,19 @@ int WebSocketConnectHandler(const struct mg_connection * conn, void *cbdata)
|
|||||||
void WebSocketReadyHandler(const struct mg_connection * conn, void *cbdata)
|
void WebSocketReadyHandler(const struct mg_connection * conn, void *cbdata)
|
||||||
{
|
{
|
||||||
struct mg_context *ctx = mg_get_context((struct mg_connection *) /* TODO: check const_casts */ conn);
|
struct mg_context *ctx = mg_get_context((struct mg_connection *) /* TODO: check const_casts */ conn);
|
||||||
|
int i;
|
||||||
|
|
||||||
const char * text = "Hello from the websocket ready handler";
|
const char * text = "Hello from the websocket ready handler";
|
||||||
/* TODO: check "const struct mg_connection *" vs "struct mg_connection *" everywhere */
|
/* TODO: check "const struct mg_connection *" vs "struct mg_connection *" everywhere */
|
||||||
mg_websocket_write((struct mg_connection *)conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text));
|
mg_websocket_write((struct mg_connection *)conn, WEBSOCKET_OPCODE_TEXT, text, strlen(text));
|
||||||
fprintf(stdout, "Client added to the set of webserver connections\r\n\r\n");
|
fprintf(stdout, "Client added to the set of webserver connections\r\n\r\n");
|
||||||
mg_lock_context(ctx);
|
mg_lock_context(ctx);
|
||||||
ws_client = conn;
|
for (i=0; i<MAX_WS_CLIENTS; i++) {
|
||||||
|
if (ws_clients[i] == NULL) {
|
||||||
|
ws_clients[i] = conn;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
mg_unlock_context(ctx);
|
mg_unlock_context(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -156,9 +163,15 @@ int WebsocketDataHandler(const struct mg_connection * conn, int bits, char * dat
|
|||||||
void WebSocketCloseHandler(const struct mg_connection * conn, void *cbdata)
|
void WebSocketCloseHandler(const struct mg_connection * conn, void *cbdata)
|
||||||
{
|
{
|
||||||
struct mg_context *ctx = mg_get_context((struct mg_connection *) /* TODO: check const_casts */ conn);
|
struct mg_context *ctx = mg_get_context((struct mg_connection *) /* TODO: check const_casts */ conn);
|
||||||
|
int i;
|
||||||
|
|
||||||
mg_lock_context(ctx);
|
mg_lock_context(ctx);
|
||||||
ws_client = NULL;
|
for (i=0; i<MAX_WS_CLIENTS; i++) {
|
||||||
|
if (ws_clients[i] == conn) {
|
||||||
|
ws_clients[i] = NULL;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
mg_unlock_context(ctx);
|
mg_unlock_context(ctx);
|
||||||
fprintf(stdout, "Client droped from the set of webserver connections\r\n\r\n");
|
fprintf(stdout, "Client droped from the set of webserver connections\r\n\r\n");
|
||||||
}
|
}
|
||||||
@ -166,11 +179,15 @@ void WebSocketCloseHandler(const struct mg_connection * conn, void *cbdata)
|
|||||||
void InformWebsockets(struct mg_context *ctx)
|
void InformWebsockets(struct mg_context *ctx)
|
||||||
{
|
{
|
||||||
char text[32];
|
char text[32];
|
||||||
|
int i;
|
||||||
|
|
||||||
|
sprintf(text, "%lu", ++cnt);
|
||||||
|
|
||||||
mg_lock_context(ctx);
|
mg_lock_context(ctx);
|
||||||
if (ws_client) {
|
for (i=0; i<MAX_WS_CLIENTS; i++) {
|
||||||
sprintf(text, "%lu", ++cnt);
|
if (ws_clients[i] != NULL) {
|
||||||
mg_websocket_write(ws_client, WEBSOCKET_OPCODE_TEXT, text, strlen(text));
|
mg_websocket_write(ws_clients[i], WEBSOCKET_OPCODE_TEXT, text, strlen(text));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
mg_unlock_context(ctx);
|
mg_unlock_context(ctx);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user