Browse Source

add connection event

moononournation 7 years ago
parent
commit
221b5b4546
1 changed files with 12 additions and 6 deletions
  1. 12 6
      bin/httpserver-websocket.lua

+ 12 - 6
bin/httpserver-websocket.lua

@@ -68,12 +68,6 @@ local function acceptKey(key)
   return crypto.toBase64(crypto.hash("sha1", key .. guid))
 end
 
-local function callback(socket)
-  function socket.onmessage(payload, opcode)
-        socket.send(payload..'!', 1)
-  end
-end
-
 return function (connection, payload)
   local buffer = false
   local socket = {}
@@ -110,6 +104,18 @@ return function (connection, payload)
     end
   end)
 
+  connection:on("sent", function(_, _)
+    if socket.onsent ~= nil then
+      socket.onsent()
+    end
+  end)
+
+  connection:on("disconnection", function(_, _)
+    if socket.onclose ~= nil then
+      socket.onclose()
+    end
+  end)
+
   local req = dofile("httpserver-request.lc")(payload)
   local key = payload:match("Sec%-WebSocket%-Key: ([A-Za-z0-9+/=]+)")
   local fileExists = file.open(req.uri.file, "r")