Bläddra i källkod

Rewrite websocket for Lua (Step 8 of ?)

bel 11 år sedan
förälder
incheckning
6e8415d4e5
2 ändrade filer med 30 tillägg och 8 borttagningar
  1. 25 3
      src/mod_lua.inl
  2. 5 5
      test/websocket.lua

+ 25 - 3
src/mod_lua.inl

@@ -1086,12 +1086,22 @@ static int lua_websocket_data(struct mg_connection * conn, void *ws_arg, int bit
     assert(ws->state != NULL);
 
     (void)pthread_mutex_lock(&ws->ws_mutex);
+
     lua_getglobal(ws->state, "data");
+    lua_newtable(ws->state);
+    lua_pushstring(ws->state, "client");
+    lua_pushlightuserdata(ws->state, (void *)conn);
+    lua_rawset(ws->state, -3);
+    lua_pushstring(ws->state, "bits"); /* TODO: dont use "bits" but fields with a meaning according to http://tools.ietf.org/html/rfc6455, section 5.2 */
     lua_pushnumber(ws->state, bits);
+    lua_rawset(ws->state, -3);
+    lua_pushstring(ws->state, "data");
     lua_pushlstring(ws->state, data, data_len);
-    err = lua_pcall(ws->state, 2, 1, 0);
+    lua_rawset(ws->state, -3);
+
+    err = lua_pcall(ws->state, 1, 1, 0);
     if (err != 0) {
-        websock_cry(conn, err, ws->state, ws->script, "open handler");
+        websock_cry(conn, err, ws->state, ws->script, "data handler");
     } else {
         if (lua_isboolean(ws->state, -1)) {
             ok = lua_toboolean(ws->state, -1);
@@ -1112,9 +1122,14 @@ static int lua_websocket_ready(struct mg_connection * conn, void * ws_arg)
     assert(ws->state != NULL);
 
     (void)pthread_mutex_lock(&ws->ws_mutex);
+
     lua_getglobal(ws->state, "ready");
     lua_newtable(ws->state);
     prepare_lua_request_info(conn, ws->state);
+    lua_pushstring(ws->state, "client");
+    lua_pushlightuserdata(ws->state, (void *)conn);
+    lua_rawset(ws->state, -3);
+
     err = lua_pcall(ws->state, 1, 1, 0);
     if (err != 0) {
         websock_cry(conn, err, ws->state, ws->script, "ready handler");
@@ -1124,6 +1139,7 @@ static int lua_websocket_ready(struct mg_connection * conn, void * ws_arg)
         }
         lua_pop(ws->state, 1);
     }
+
     (void)pthread_mutex_unlock(&ws->ws_mutex);
 
     return ok;
@@ -1140,8 +1156,14 @@ static void lua_websocket_close(struct mg_connection * conn, void * ws_arg)
     assert(ws->state != NULL);
 
     (void)pthread_mutex_lock(&ws->ws_mutex);
+
     lua_getglobal(ws->state, "close");
-    err = lua_pcall(ws->state, 0, 0, 0);
+    lua_newtable(ws->state);
+    lua_pushstring(ws->state, "client");
+    lua_pushlightuserdata(ws->state, (void *)conn);
+    lua_rawset(ws->state, -3);
+
+    err = lua_pcall(ws->state, 1, 0, 0);
     if (err != 0) {
         websock_cry(conn, err, ws->state, ws->script, "close handler");
     }

+ 5 - 5
test/websocket.lua

@@ -28,7 +28,7 @@ function ser(val)
   local t
   if type(val) == "table" then
     for k,v in pairs(val) do
-      if t then 
+      if t then
         t = t .. ", " .. ser(k) .. "=" .. ser(v)
       else
         t = "{" .. ser(k) .. "=" .. ser(v)
@@ -57,15 +57,15 @@ function ready(tab)
 end
 
 -- Callback for "Websocket received data"
-function data(bits, content)
-    trace("data(" .. bits .. "): " .. content)
+function data(tab)
+    trace("data: " .. ser(tab))
     senddata()
     return true
 end
 
 -- Callback for "Websocket is closing"
-function close()
-    trace("close")
+function close(tab)
+    trace("close: " .. ser(tab))
     mg.write("text", "end")
 end