mirror of
https://github.com/lammertb/libhttp.git
synced 2025-07-31 08:24:23 +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:
@ -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);
|
||||||
|
@ -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
|
||||||
@ -6650,7 +6650,11 @@ static void handle_request(struct mg_connection *conn)
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
#if defined(USE_WEBSOCKET)
|
#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
|
#endif
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
Reference in New Issue
Block a user