Browse Source

Handler for websocket connection (Step 4/?)

Add an interface to register handler for websocket connections.
See enhancement #30 and question #101
bel 10 years ago
parent
commit
0ecec6bce7
2 changed files with 11 additions and 8 deletions
  1. 4 5
      examples/websocket_client/websocket_client.c
  2. 7 3
      src/civetweb.c

+ 4 - 5
examples/websocket_client/websocket_client.c

@@ -105,9 +105,9 @@ struct tclient_data {
     int closed;
     int closed;
 };
 };
 
 
-static int websocket_client_data_handler(struct mg_connection *conn, int flags, char *data, size_t data_len)
+static int websocket_client_data_handler(const struct mg_connection *conn, int flags, char *data, size_t data_len, void * user_data)
 {
 {
-    struct mg_context *ctx = mg_get_context(conn);
+    struct mg_context *ctx = mg_get_context(conn); /* TODO: const qualifier */
     struct tclient_data *pclient_data = (struct tclient_data *) mg_get_user_data(ctx);
     struct tclient_data *pclient_data = (struct tclient_data *) mg_get_user_data(ctx);
 
 
     printf("Client received data from server: ");
     printf("Client received data from server: ");
@@ -122,9 +122,9 @@ static int websocket_client_data_handler(struct mg_connection *conn, int flags,
     return 1;
     return 1;
 }
 }
 
 
-static void websocket_client_close_handler(struct mg_connection *conn)
+static void websocket_client_close_handler(const struct mg_connection *conn, void * user_data)
 {
 {
-    struct mg_context *ctx = mg_get_context(conn);
+    struct mg_context *ctx = mg_get_context(conn); /* TODO: const qualifier */
     struct tclient_data *pclient_data = (struct tclient_data *) mg_get_user_data(ctx);
     struct tclient_data *pclient_data = (struct tclient_data *) mg_get_user_data(ctx);
 
 
     printf("Client: Close handler\n");
     printf("Client: Close handler\n");
@@ -151,7 +151,6 @@ int main(int argc, char *argv[])
     printf("Server init\n\n");
     printf("Server init\n\n");
 
 
     /* Then connect a first client */
     /* Then connect a first client */
-    /* TODO: parameters changed -> fix them */ xxx
     newconn1 = mg_connect_websocket_client("localhost", atoi(PORT), 0, ebuf, sizeof(ebuf),
     newconn1 = mg_connect_websocket_client("localhost", atoi(PORT), 0, ebuf, sizeof(ebuf),
         "/websocket", NULL, websocket_client_data_handler, websocket_client_close_handler,
         "/websocket", NULL, websocket_client_data_handler, websocket_client_close_handler,
         &client1_data);
         &client1_data);

+ 7 - 3
src/civetweb.c

@@ -5978,7 +5978,7 @@ static void handle_websocket_request(struct mg_connection *conn,
             if (conn->lua_websocket_state) {
             if (conn->lua_websocket_state) {
                 send_websocket_handshake(conn);
                 send_websocket_handshake(conn);
                 if (lua_websocket_ready(conn, conn->lua_websocket_state)) {
                 if (lua_websocket_ready(conn, conn->lua_websocket_state)) {
-                    read_websocket(conn);
+                    read_websocket(conn, NULL, NULL);
                 }
                 }
             }
             }
         } else
         } else
@@ -6649,8 +6649,12 @@ static void handle_request(struct mg_connection *conn)
                 goto auth_check;
                 goto auth_check;
             }
             }
         } else {
         } else {
-#if defined(USE_WEBSOCKET)
-            handle_websocket_request(conn, path, is_script_resource, ws_connect_handler, ws_ready_handler, ws_data_handler, ws_close_handler, callback_data);
+#if defined(USE_WEBSOCKET)            
+            handle_websocket_request(conn, path, 
+                                     0 /* do not use is_script_resource here */, 
+                                     ws_connect_handler, ws_ready_handler, ws_data_handler, ws_close_handler, 
+                                     callback_data
+                                     );
 #endif
 #endif
         }
         }
         return;
         return;