Merge 0.10->trunk
[prosody.git] / plugins / mod_websocket.lua
index 418cd84639f0aa07df2ec6d3d9c512103eeee6be..a3f5318c9b54636f8caec1b747bf39fd56bd9033 100644 (file)
@@ -226,8 +226,7 @@ function handle_request(event)
                        frame.opcode = 0xA;
                        conn:write(build_frame(frame));
                        return "";
-               elseif opcode == 0xA then -- Pong frame
-                       module:log("warn", "Received unexpected pong frame: " .. tostring(frame.data));
+               elseif opcode == 0xA then -- Pong frame, MAY be sent unsolicited, eg as keepalive
                        return "";
                else
                        log("warn", "Received frame with unsupported opcode %i", opcode);
@@ -275,7 +274,7 @@ function handle_request(event)
                        attr["xmlns:stream"] = attr["xmlns:stream"] or xmlns_streams;
                end
                return stanza;
-       end);
+       end, -1000);
 
        add_filter(session, "bytes/out", function(data)
                return build_frame({ FIN = true, opcode = 0x01, data = tostring(data)});
@@ -291,6 +290,15 @@ function handle_request(event)
        return "";
 end
 
+local function keepalive(event)
+       local session = event.session;
+       if session.open_stream == session_open_stream then
+               return session.conn:write(build_frame({ opcode = 0x9, }));
+       end
+end
+
+module:hook("c2s-read-timeout", keepalive, -0.9);
+
 function module.add_host(module)
        module:depends("http");
        module:provides("http", {
@@ -301,4 +309,5 @@ function module.add_host(module)
                        ["GET /"] = handle_request;
                };
        });
+       module:hook("c2s-read-timeout", keepalive, -0.9);
 end