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

Update websocket example - use the current interface

This commit is contained in:
bel
2015-08-19 21:15:22 +02:00
parent 615970e9d1
commit 9f3f8baac0
3 changed files with 17 additions and 9 deletions

View File

@ -34,7 +34,7 @@ static void send_to_all_websockets(struct mg_context *ctx, const char * data, in
} }
void websocket_ready_handler(struct mg_connection *conn) { void websocket_ready_handler(struct mg_connection *conn, void *_ignored) {
int i; int i;
const struct mg_request_info * rq = mg_get_request_info(conn); const struct mg_request_info * rq = mg_get_request_info(conn);
@ -77,7 +77,7 @@ static void websocket_done(tWebSockContext *ws_ctx, 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, void *_ignored) {
const struct mg_request_info * rq = mg_get_request_info(conn); const struct mg_request_info * rq = mg_get_request_info(conn);
tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data; tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
@ -129,7 +129,7 @@ int websocket_data_handler(struct mg_connection *conn, int flags, char *data, si
} }
void connection_close_handler(const struct mg_connection *conn) { void connection_close_handler(const struct mg_connection *conn, void *_ignored) {
const struct mg_request_info * rq = mg_get_request_info(conn); const struct mg_request_info * rq = mg_get_request_info(conn);
tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data; tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
@ -162,6 +162,7 @@ static void * eventMain(void * arg) {
return NULL; return NULL;
} }
void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_len) { void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_len) {
char buffer[260]; char buffer[260];
@ -174,6 +175,7 @@ void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_
} }
} }
void websock_init_lib(const struct mg_context *ctx) { void websock_init_lib(const struct mg_context *ctx) {
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx); tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);
@ -182,6 +184,7 @@ void websock_init_lib(const struct mg_context *ctx) {
mg_start_thread(eventMain, (void*)ctx); mg_start_thread(eventMain, (void*)ctx);
} }
void websock_exit_lib(const struct mg_context *ctx) { void websock_exit_lib(const struct mg_context *ctx) {
tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx); tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);

View File

@ -27,9 +27,9 @@ void websock_exit_lib(const struct mg_context *ctx);
void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_len); void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_len);
void websocket_ready_handler(struct mg_connection *conn); void websocket_ready_handler(struct mg_connection *conn, void *_ignored);
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, void *_ignored);
void connection_close_handler(const struct mg_connection *conn); void connection_close_handler(const struct mg_connection *conn, void *_ignored);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -30,11 +30,16 @@ int main(void)
callback_funcs.init_context = websock_init_lib; callback_funcs.init_context = websock_init_lib;
callback_funcs.exit_context = websock_exit_lib; callback_funcs.exit_context = websock_exit_lib;
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, &ws_ctx, server_options); ctx = mg_start(&callback_funcs, &ws_ctx, server_options);
mg_set_websocket_handler(ctx, "/MyWebSock",
NULL,
websocket_ready_handler,
websocket_data_handler,
connection_close_handler,
NULL);
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:");