Class: Ably::Realtime::Connection::WebsocketTransport Private

Inherits:
EventMachine::Connection
  • Object
show all
Extended by:
Modules::Enum
Includes:
Modules::EventEmitter, Modules::StateEmitter
Defined in:
lib/ably/realtime/connection/websocket_transport.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

EventMachine WebSocket transport

Constant Summary collapse

STATE =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Valid WebSocket connection states

ruby_enum('STATE',
  :initialized,
  :connecting,
  :connected,
  :disconnecting,
  :disconnected
)

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Modules::StateEmitter

#once_or_if, #once_state_changed, #state, #state=, #state?, #unsafe_once_or_if, #unsafe_once_state_changed

Methods included from Modules::EventEmitter

#emit, #off, #on, #once, #unsafe_on, #unsafe_once

Constructor Details

#initialize(connection, url) ⇒ WebsocketTransport

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of WebsocketTransport.



19
20
21
22
23
24
25
# File 'lib/ably/realtime/connection/websocket_transport.rb', line 19

def initialize(connection, url)
  @connection = connection
  @state      = STATE.Initialized
  @url        = url

  setup_event_handlers
end

Instance Attribute Details

#__incoming_protocol_msgbus__Ably::Util::PubSub (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Websocket Transport internal incoming protocol message bus.

Returns:



89
90
91
# File 'lib/ably/realtime/connection/websocket_transport.rb', line 89

def __incoming_protocol_msgbus__
  @__incoming_protocol_msgbus__ ||= create_pub_sub_message_bus
end

#__outgoing_protocol_msgbus__Ably::Util::PubSub (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns Websocket Transport internal outgoing protocol message bus.

Returns:



96
97
98
# File 'lib/ably/realtime/connection/websocket_transport.rb', line 96

def __outgoing_protocol_msgbus__
  @__outgoing_protocol_msgbus__ ||= create_pub_sub_message_bus
end

Instance Method Details

#connection_completedObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Remote TCP connection attempt completes successfully Required EventMachine::Connection interface



50
51
52
53
54
# File 'lib/ably/realtime/connection/websocket_transport.rb', line 50

def connection_completed
  change_state STATE.Connected
  start_tls if client.use_tls?
  driver.start
end

#disconnectvoid

This method returns an undefined value.

Disconnect the socket transport connection and write all pending text. If Disconnected state is not automatically emitted, it will be emitted automatically



31
32
33
34
35
36
37
38
# File 'lib/ably/realtime/connection/websocket_transport.rb', line 31

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 unless disconnected?
  end
end

#post_initObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Network connection has been established Required EventMachine::Connection interface



42
43
44
45
46
# File 'lib/ably/realtime/connection/websocket_transport.rb', line 42

def post_init
  clear_timer
  change_state STATE.Connecting
  setup_driver
end

#ready_for_release?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

True if socket connection is ready to be released i.e. it is not currently connecting or connected

Returns:

  • (Boolean)


82
83
84
# File 'lib/ably/realtime/connection/websocket_transport.rb', line 82

def ready_for_release?
  !connecting? && !connected?
end

#receive_data(data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Called by the event loop whenever data has been received by the network connection. Simply pass onto the WebSocket driver to process and determine content boundaries. Required EventMachine::Connection interface



59
60
61
# File 'lib/ably/realtime/connection/websocket_transport.rb', line 59

def receive_data(data)
  driver.parse(data)
end

#unbindObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Called whenever a connection (either a server or client connection) is closed Required EventMachine::Connection interface



65
66
67
# File 'lib/ably/realtime/connection/websocket_transport.rb', line 65

def unbind
  change_state STATE.Disconnected, reason_closed || 'Websocket connection closed unexpectedly'
end

#urlObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

URL end point including initialization configuration WebSocket::Driver interface



71
72
73
# File 'lib/ably/realtime/connection/websocket_transport.rb', line 71

def url
  @url
end

#write(data) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

WebSocket::Driver interface



76
77
78
# File 'lib/ably/realtime/connection/websocket_transport.rb', line 76

def write(data)
  send_data(data)
end