소스 검색

Rewrite websockets for Lua (Step 6 of ?)

bel 11 년 전
부모
커밋
dbcae1b0e8
2개의 변경된 파일11개의 추가작업 그리고 8개의 파일을 삭제
  1. 5 4
      src/mod_lua.inl
  2. 6 4
      test/websocket.lua

+ 5 - 4
src/mod_lua.inl

@@ -740,12 +740,12 @@ static void prepare_lua_environment(struct mg_context * ctx, struct mg_connectio
     /* Store context in the registry */
     if (ctx) {
         lua_pushlightuserdata(L, (void *)&lua_regkey_ctx);
-        lua_pushlightuserdata(L, (void *)&(ctx));
+        lua_pushlightuserdata(L, (void *)ctx);
         lua_settable(L, LUA_REGISTRYINDEX);
     }
     if (conn_list) {
         lua_pushlightuserdata(L, (void *)&lua_regkey_connlist);
-        lua_pushlightuserdata(L, (void *)&(conn_list));
+        lua_pushlightuserdata(L, (void *)conn_list);
         lua_settable(L, LUA_REGISTRYINDEX);
     }
 
@@ -838,9 +838,10 @@ static void prepare_lua_environment(struct mg_context * ctx, struct mg_connectio
             reg_string(L, conn->request_info.http_headers[i].name, conn->request_info.http_headers[i].value);
         }
         lua_rawset(L, -3);
+
+        lua_rawset(L, -3);
     }
 
-    lua_rawset(L, -3);
     lua_setglobal(L, "mg");
 
     /* Register default mg.onerror function */
@@ -1017,7 +1018,7 @@ static void * lua_websocket_new(const char * script, struct mg_connection *conn)
     while (*shared_websock_list) {
         /* check if ws already in list */
         if (0==strcmp(script,(*shared_websock_list)->ws.script)) {
-            break; /* -- TODO: shared websocket does not work yet, disable it by removing this "break" statement */
+            break;
         }
         shared_websock_list = &((*shared_websock_list)->next);
     }

+ 6 - 4
test/websocket.lua

@@ -5,11 +5,14 @@ function trace(text)
 end
 
 function iswebsocket()
-  return pcall(function()
-    if (string.upper(mg.request_info.http_headers.Upgrade)~="WEBSOCKET") then error("") end
-  end)
+  return mg.lua_type == "websocket"
+  --return pcall(function()
+  --  if (string.upper(mg.request_info.http_headers.Upgrade)~="WEBSOCKET") then error("") end
+  --end)
 end
 
+trace("called with Lua type " .. tostring(mg.lua_type))
+
 if not iswebsocket() then
   trace("no websocket")
   mg.write("HTTP/1.0 403 Forbidden\r\n")
@@ -65,4 +68,3 @@ function senddata()
     end
 end
 
-trace("defined")