lib/ably/realtime/connection/websocket_transport.rb in ably-0.8.15 vs lib/ably/realtime/connection/websocket_transport.rb in ably-1.0.0

- old
+ new

@@ -117,18 +117,18 @@ when :json driver.text(object.to_json) when :msgpack driver.binary(object.to_msgpack.unpack('C*')) else - client.logger.fatal "WebsocketTransport: Unsupported protocol '#{client.protocol}' for serialization, object cannot be serialized and sent to Ably over this WebSocket" + client.logger.fatal { "WebsocketTransport: Unsupported protocol '#{client.protocol}' for serialization, object cannot be serialized and sent to Ably over this WebSocket" } end end def setup_event_handlers __outgoing_protocol_msgbus__.subscribe(:protocol_message) do |protocol_message| send_object protocol_message - client.logger.debug "WebsocketTransport: Prot msg sent =>: #{protocol_message.action} #{protocol_message}" + client.logger.debug { "WebsocketTransport: Prot msg sent =>: #{protocol_message.action} #{protocol_message}" } end end def clear_timer if defined?(@timer) && @timer @@ -145,36 +145,45 @@ def setup_driver @driver = WebSocket::Driver.client(self) driver.on("open") do - logger.debug "WebsocketTransport: socket opened to #{url}, waiting for Connected protocol message" + 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: logger) action_name = Ably::Models::ProtocolMessage::ACTION[event_data['action']] rescue event_data['action'] - logger.debug "WebsocketTransport: Prot msg recv <=: #{action_name} - #{event_data}" + logger.debug { "WebsocketTransport: Prot msg recv <=: #{action_name} - #{event_data}" } if protocol_message.invalid? - error = Ably::Exceptions::ProtocolError.new("Invalid Protocol Message received: #{event_data}\nMessage has been discarded", 400, 80013) - connection.emit :error, error - logger.fatal "WebsocketTransport: #{error.message}" + error = Ably::Exceptions::ProtocolError.new("Invalid Protocol Message received: #{event_data}\nConnection moving to the failed state as the protocol is invalid and unsupported", 400, 80013) + logger.fatal { "WebsocketTransport: #{error.message}" } + failed_protocol_message = Ably::Models::ProtocolMessage.new( + action: Ably::Models::ProtocolMessage::ACTION.Error, + error: error.as_json, + logger: logger + ) + __incoming_protocol_msgbus__.publish :protocol_message, failed_protocol_message else __incoming_protocol_msgbus__.publish :protocol_message, protocol_message end end + driver.on("ping") do + __incoming_protocol_msgbus__.publish :protocol_message, Ably::Models::ProtocolMessage.new(action: Ably::Models::ProtocolMessage::ACTION.Heartbeat, source: :websocket) + end + driver.on("error") do |error| - logger.error "WebsocketTransport: Protocol Error on transports - #{error.message}" + 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}" + logger.warn { "WebsocketTransport: Driver reported transport as closed - #{reason_closed}" } end end def client connection.client @@ -190,10 +199,10 @@ when :json JSON.parse(data) when :msgpack MessagePack.unpack(data.pack('C*')) else - client.logger.fatal "WebsocketTransport: Unsupported Protocol Message format #{client.protocol}" + client.logger.fatal { "WebsocketTransport: Unsupported Protocol Message format #{client.protocol}" } data end end def create_pub_sub_message_bus