Selaa lähdekoodia

Update websocket example - use the current interface

bel 10 vuotta sitten
vanhempi
commit
9f3f8baac0

+ 6 - 3
examples/websocket/WebSockCallbacks.c

@@ -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;
     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);
     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);
     tWebSockInfo * wsock = (tWebSockInfo*)rq->conn_data;
@@ -162,6 +162,7 @@ static void * eventMain(void * arg) {
     return NULL;
 }
 
+
 void websock_send_broadcast(struct mg_context *ctx, const char * data, int data_len) {
 
     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) {
 
     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);
 }
 
+
 void websock_exit_lib(const struct mg_context *ctx) {
 
     tWebSockContext *ws_ctx = (tWebSockContext*) mg_get_user_data(ctx);

+ 3 - 3
examples/websocket/WebSockCallbacks.h

@@ -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 websocket_ready_handler(struct mg_connection *conn);
-int websocket_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len);
-void connection_close_handler(const 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, void *_ignored);
+void connection_close_handler(const struct mg_connection *conn, void *_ignored);
 
 
 #ifdef __cplusplus

+ 8 - 3
examples/websocket/websocket.c

@@ -30,11 +30,16 @@ int main(void)
 
     callback_funcs.init_context = websock_init_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);
+
+    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"));
 
     puts("Enter an (ASCII) character or * to exit:");