lib/em-websocket/handler.rb in em-websocket-0.5.0 vs lib/em-websocket/handler.rb in em-websocket-0.5.1
- old
+ new
@@ -36,30 +36,52 @@
def initialize(connection, debug = false)
@connection = connection
@debug = debug
@state = :connected
+ @close_timer = nil
initialize_framing
end
def receive_data(data)
@data << data
- process_data(data)
+ process_data
+ rescue WSProtocolError => e
+ fail_websocket(e)
end
def close_websocket(code, body)
# Implemented in subclass
end
+ # Used to avoid un-acked and unclosed remaining open indefinitely
+ def start_close_timeout
+ @close_timer = EM::Timer.new(@connection.close_timeout) {
+ @connection.close_connection
+ e = WSProtocolError.new("Close handshake un-acked after #{@connection.close_timeout}s, closing tcp connection")
+ @connection.trigger_on_error(e)
+ }
+ end
+
+ # This corresponds to "Fail the WebSocket Connection" in the spec.
+ def fail_websocket(e)
+ debug [:error, e]
+ close_websocket(e.code, e.message)
+ @connection.close_connection_after_writing
+ @connection.trigger_on_error(e)
+ end
+
def unbind
@state = :closed
+ @close_timer.cancel if @close_timer
+
@close_info = defined?(@close_info) ? @close_info : {
:code => 1006,
:was_clean => false,
}
- @connection.trigger_on_close(@close_info )
+ @connection.trigger_on_close(@close_info)
end
def ping
# Overridden in subclass
false