From 0ecec6bce74e2ebc7403b824341a9d83c570e85d Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 19 Apr 2015 14:39:15 +0200 Subject: [PATCH] Handler for websocket connection (Step 4/?) Add an interface to register handler for websocket connections. See enhancement #30 and question #101 --- examples/websocket_client/websocket_client.c | 9 ++++----- src/civetweb.c | 10 +++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/examples/websocket_client/websocket_client.c b/examples/websocket_client/websocket_client.c index b78ab0ec..bd6ad901 100644 --- a/examples/websocket_client/websocket_client.c +++ b/examples/websocket_client/websocket_client.c @@ -105,9 +105,9 @@ struct tclient_data { 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); printf("Client received data from server: "); @@ -122,9 +122,9 @@ static int websocket_client_data_handler(struct mg_connection *conn, int flags, 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); printf("Client: Close handler\n"); @@ -151,7 +151,6 @@ int main(int argc, char *argv[]) printf("Server init\n\n"); /* Then connect a first client */ - /* TODO: parameters changed -> fix them */ xxx newconn1 = mg_connect_websocket_client("localhost", atoi(PORT), 0, ebuf, sizeof(ebuf), "/websocket", NULL, websocket_client_data_handler, websocket_client_close_handler, &client1_data); diff --git a/src/civetweb.c b/src/civetweb.c index 3f130220..7e468a70 100755 --- a/src/civetweb.c +++ b/src/civetweb.c @@ -5978,7 +5978,7 @@ static void handle_websocket_request(struct mg_connection *conn, if (conn->lua_websocket_state) { send_websocket_handshake(conn); if (lua_websocket_ready(conn, conn->lua_websocket_state)) { - read_websocket(conn); + read_websocket(conn, NULL, NULL); } } } else @@ -6649,8 +6649,12 @@ static void handle_request(struct mg_connection *conn) goto auth_check; } } 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 } return;