1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-07-29 21:01:13 +03:00

Handler for websocket connection (Step 4/?)

Add an interface to register handler for websocket connections.
See enhancement #30 and question #101
This commit is contained in:
bel
2015-04-19 14:39:15 +02:00
parent d64d507eaa
commit 0ecec6bce7
2 changed files with 11 additions and 8 deletions

View File

@ -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);

View File

@ -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
@ -6650,7 +6650,11 @@ static void handle_request(struct mg_connection *conn)
}
} 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);
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;