~glacambre/firenvim

553c913db02952f305e71e76aa0a8f6c71eb83ff — glacambre 2 years ago eb3abef
websocket server: send pong on ping

Hopefully closes https://github.com/glacambre/firenvim/issues/1222.
2 files changed, 10 insertions(+), 2 deletions(-)

M lua/firenvim-websocket.lua
M lua/firenvim.lua
M lua/firenvim-websocket.lua => lua/firenvim-websocket.lua +9 -0
@@ 137,6 137,14 @@ local function encode_frame(data)
        return  header .. len .. data
end

local function pong_frame(decoded_frame)
        -- 137: 10001010
        -- Fin: 1
        -- RSV{1,2,3}: 0
        -- Opcode: 0xA (pong)
        return string.char(138) .. decoded_frame.payload_length .. decoded_frame.payload_data
end

local function close_frame()
        local frame = encode_frame("")
        return string.char(136) .. string.sub(frame, 2)


@@ 149,4 157,5 @@ return {
        encode_frame = encode_frame,
        opcodes = opcodes,
        parse_headers = parse_headers,
        pong_frame = pong_frame,
}

M lua/firenvim.lua => lua/firenvim.lua +1 -2
@@ 68,8 68,7 @@ local function connection_handler(server, sock, token)
                                        current_payload = ""
                                end
                        elseif decoded_frame.opcode == websocket.opcodes.ping then
                                -- TODO: implement websocket.pong_frame
                                -- sock:write(websocket.pong_frame(decoded_frame))
                                sock:write(websocket.pong_frame(decoded_frame))
                                return
                        elseif decoded_frame.opcode == websocket.opcodes.close then
                                sock:write(websocket.close_frame(decoded_frame))