lib/submodules/ably-ruby/lib/ably/realtime/connection/websocket_transport.rb in ably-rest-0.7.3 vs lib/submodules/ably-ruby/lib/ably/realtime/connection/websocket_transport.rb in ably-rest-0.7.5
- old
+ new
@@ -31,11 +31,11 @@
def disconnect
close_connection_after_writing
change_state STATE.Disconnecting
create_timer(2) do
# if connection is not disconnected within 2s, set state as disconnected
- change_state STATE.Disconnected
+ change_state STATE.Disconnected unless disconnected?
end
end
# Network connection has been established
# Required {http://www.rubydoc.info/github/eventmachine/eventmachine/EventMachine/Connection EventMachine::Connection} interface
@@ -61,11 +61,11 @@
end
# Called whenever a connection (either a server or client connection) is closed
# Required {http://www.rubydoc.info/github/eventmachine/eventmachine/EventMachine/Connection EventMachine::Connection} interface
def unbind
- change_state STATE.Disconnected
+ change_state STATE.Disconnected, reason_closed || 'Websocket connection closed unexpectedly'
end
# URL end point including initialization configuration
# {http://www.rubydoc.info/gems/websocket-driver/0.3.5/WebSocket/Driver WebSocket::Driver} interface
def url
@@ -96,11 +96,11 @@
def __outgoing_protocol_msgbus__
@__outgoing_protocol_msgbus__ ||= create_pub_sub_message_bus
end
private
- attr_reader :connection, :driver
+ attr_reader :connection, :driver, :reason_closed
# Send object down the WebSocket driver connection as a serialized string/byte array based on protocol
# @param [Object] object to serialize and send to the WebSocket driver
# @api public
def send_object(object)
@@ -141,17 +141,30 @@
logger.debug "WebsocketTransport: socket opened to #{url}, waiting for Connected protocol message"
end
driver.on("message") do |event|
event_data = parse_event_data(event.data).freeze
- protocol_message = Ably::Models::ProtocolMessage.new(event_data)
- logger.debug "WebsocketTransport: Prot msg recv <=: #{protocol_message.action} #{event_data}"
+ protocol_message = Ably::Models::ProtocolMessage.new(event_data, logger: logger)
+ action_name = Ably::Models::ProtocolMessage::ACTION[event_data['action']] rescue event_data['action']
+ logger.debug "WebsocketTransport: Prot msg recv <=: #{action_name} - #{event_data}"
if protocol_message.invalid?
- logger.fatal "WebsocketTransport: Invalid Protocol Message received: #{event_data}\nNo action taken"
+ error = Ably::Exceptions::ProtocolError.new("Invalid Protocol Message received: #{event_data}\nMessage has been discarded", 400, 80013)
+ connection.trigger :error, error
+ logger.fatal "WebsocketTransport: #{error.message}"
else
__incoming_protocol_msgbus__.publish :protocol_message, protocol_message
end
+ end
+
+ driver.on("error") do |error|
+ logger.error "WebsocketTransport: Protocol Error on transports - #{error.message}"
+ end
+
+ @reason_closed = nil
+ driver.on("closed") do |event|
+ @reason_closed = "#{event.code}: #{event.reason}"
+ logger.warn "WebsocketTransport: Driver reported transport as closed - #{reason_closed}"
end
end
def client
connection.client