1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-07-31 08:24:23 +03:00

Handler for websocket connection (Step 7/7)

Add an interface to register handler for websocket connections.
See enhancement #30 and question #101
This commit is contained in:
bel
2015-04-19 23:20:08 +02:00
parent 121ae6c7f6
commit 8c90b3cae5
7 changed files with 29 additions and 19 deletions

View File

@ -5,6 +5,7 @@ Release Notes v1.7 (Under Development)
Changes
-------
- URI specific callbacks for websockets
- Add chunked transfer support
- Update LuaFileSystem
- Update Lua to 5.2.4

View File

@ -52,7 +52,7 @@ By default, the server will automatically serve up files like a normal HTTP serv
Lua Support
------
Lua is a server side include functionality. Files ending in .la will be processed with Lua.
Lua is a server side include functionality. Files ending in .lua will be processed with Lua.
##### Add the following CFLAGS

View File

@ -1,3 +1,8 @@
/* This example uses deprecated interfaces: global websocket callbacks.
They have been superseeded by URI specific callbacks.
See examples/embedded_c for an up to date example.
*/
#include <assert.h>
#include <stdlib.h>
#include <time.h>

View File

@ -1,3 +1,8 @@
/* This example uses deprecated interfaces: global websocket callbacks.
They have been superseeded by URI specific callbacks.
See examples/embedded_c for an up to date example.
*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014 the Civetweb developers
* Copyright (c) 2014-2015 the Civetweb developers
* Copyright (c) 2014 Jordan Shelley
* https://github.com/jshelley
* License http://opensource.org/licenses/mit-license.php MIT License

View File

@ -5865,19 +5865,11 @@ static void read_websocket(struct mg_connection *conn, mg_websocket_data_handler
}
}
/* Exit the loop if callback signalled to exit,
or "connection close" opcode received. */
/* Exit the loop if callback signals to exit (server side),
or "connection close" opcode received (client side). */
exit_by_callback = 0;
if ((ws_data_handler != NULL &&
#ifdef USE_LUA
(conn->lua_websocket_state == NULL) &&
#endif
!ws_data_handler(conn, mop, data, data_len, callback_data)) ||
#ifdef USE_LUA
(conn->lua_websocket_state &&
!lua_websocket_data(conn, conn->lua_websocket_state, mop, data, data_len)) ||
#endif
0) {
if ((ws_data_handler != NULL) &&
!ws_data_handler(conn, mop, data, data_len, callback_data)) {
exit_by_callback = 1;
}
@ -6029,8 +6021,10 @@ static void handle_websocket_request(struct mg_connection *conn,
/* Step 7: Enter the read loop */
if (is_callback_resource) {
read_websocket(conn, ws_data_handler, cbData);
#if defined(USE_LUA)
} else if (lua_websock) {
read_websocket(conn, NULL, NULL);
read_websocket(conn, lua_websocket_data, conn->lua_websocket_state);
#endif
}
/* Step 8: Call the close handler */
@ -6704,7 +6698,8 @@ static void handle_request(struct mg_connection *conn)
/* 8. handle websocket requests */
#if defined(USE_WEBSOCKET)
if (is_websocket_request) {
handle_websocket_request(conn, path, is_callback_resource,
handle_websocket_request(conn, path,
!is_script_resource /* could be deprecated global callback */,
deprecated_websocket_connect_wrapper,
deprecated_websocket_ready_wrapper,
deprecated_websocket_data_wrapper,
@ -7739,10 +7734,14 @@ struct mg_connection *mg_connect_websocket_client(const char *host, int port, in
conn = mg_download(host, port, use_ssl,
error_buffer, error_buffer_size,
handshake_req, path, host, magic, origin);
<EFBFBD>
/* Connection object will be null if something goes wrong */
if (conn == NULL || (strcmp(conn->request_info.uri, "101") != 0))
{
if (!*error_buffer) {
/* if there is a connection, but it did not return 101, error_buffer is not yet set */
mg_snprintf(conn, error_buffer, error_buffer_size, "Unexpected server reply");
}
DEBUG_TRACE("Websocket client connect error: %s\r\n", error_buffer);
if (conn != NULL) { mg_free(conn); conn = NULL; }
return conn;

View File

@ -1306,7 +1306,7 @@ static void * lua_websocket_new(const char * script, struct mg_connection *conn)
return ok ? (void*)ws : NULL;
}
static int lua_websocket_data(struct mg_connection * conn, void *ws_arg, int bits, char *data, size_t data_len)
static int lua_websocket_data(struct mg_connection * conn, int bits, char *data, size_t data_len, void *ws_arg)
{
struct lua_websock_data *ws = (struct lua_websock_data *)(ws_arg);
int err, ok = 0;