lib/faye/websocket/hybi_parser.rb in faye-websocket-0.3.0 vs lib/faye/websocket/hybi_parser.rb in faye-websocket-0.4.0

- old
+ new

@@ -46,12 +46,13 @@ @socket = web_socket @reader = StreamReader.new @stage = 0 @masking = options[:masking] @protocols = options[:protocols] - @protocols = @protocols.split(/\s*,\s*/) if String === @protocols + + @ping_callbacks = {} end def version "hybi-#{@socket.env['HTTP_SEC_WEBSOCKET_VERSION']}" end @@ -85,11 +86,15 @@ end def create_handshake Handshake.new(@socket.uri, @protocols) end - + + def open? + true + end + def parse(data) @reader.put(data.bytes.to_a) buffer = true while buffer case @stage @@ -170,10 +175,15 @@ frame.concat(buffer) WebSocket.encode(frame) end + + def ping(message = '', &callback) + @ping_callbacks[message] = callback if callback + @socket.send(message, :ping) + end def close(code = nil, reason = nil, &callback) return if @closed @closing_callback ||= callback @socket.send(reason || '', :close, code || ERRORS[:normal_closure]) @@ -281,9 +291,15 @@ @closing_callback.call if @closing_callback when OPCODES[:ping] then return @socket.close(ERRORS[:protocol_error], nil, false) if payload.size > 125 @socket.send(payload, :pong) + + when OPCODES[:pong] then + message = WebSocket.encode(payload, true) + callback = @ping_callbacks[message] + @ping_callbacks.delete(message) + callback.call if callback end end def reset @buffer = []