소스 검색

Handler for websocket connection (Step 7/7)

Add an interface to register handler for websocket connections.
See enhancement #30 and question #101
bel 10 년 전
부모
커밋
8c90b3cae5
7개의 변경된 파일29개의 추가작업 그리고 19개의 파일을 삭제
  1. 1 0
      RELEASE_NOTES.md
  2. 1 1
      docs/Embedding.md
  3. 5 0
      examples/websocket/WebSockCallbacks.c
  4. 5 0
      examples/websocket/websocket.c
  5. 1 1
      examples/websocket_client/websocket_client.c
  6. 15 16
      src/civetweb.c
  7. 1 1
      src/mod_lua.inl

+ 1 - 0
RELEASE_NOTES.md

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

+ 1 - 1
docs/Embedding.md

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

+ 5 - 0
examples/websocket/WebSockCallbacks.c

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

+ 5 - 0
examples/websocket/websocket.c

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

+ 1 - 1
examples/websocket_client/websocket_client.c

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

+ 15 - 16
src/civetweb.c

@@ -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,12 +7734,16 @@ 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);
-
+                             ä
     /* 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; }
+        if (conn != NULL) { mg_free(conn); conn = NULL; }
         return conn;
     }
 

+ 1 - 1
src/mod_lua.inl

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