Class: RocketChat::Realtime::Client

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
EventEmitter, Methods::Auth, Methods::Message, Subscriptions::Room
Defined in:
lib/rocket_chat/realtime/client.rb

Overview

Rocket.Chat Reamtime API

Since:

  • 0.1.0

Constant Summary collapse

INITIALIZE_COMMAND =

Since:

  • 0.1.0

{
  msg: :connect,
  version: '1',
  support: ['1']
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Subscriptions::Room

#subscribe_room_messages

Methods included from Methods::Message

#send_message

Methods included from Methods::Auth

#login

Methods included from EventEmitter

#emit, #off, #on

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.

Parameters:

  • options (Hash) (defaults to: {})

Since:

  • 0.1.0



41
42
43
44
# File 'lib/rocket_chat/realtime/client.rb', line 41

def initialize(options = {})
  @server = options[:server]
  @connector = Connector.new(endpoint)
end

Instance Attribute Details

#adapterObject (readonly)

Since:

  • 0.1.0



36
37
38
# File 'lib/rocket_chat/realtime/client.rb', line 36

def adapter
  @adapter
end

#connectorObject (readonly)

Since:

  • 0.1.0



36
37
38
# File 'lib/rocket_chat/realtime/client.rb', line 36

def connector
  @connector
end

#dispatcherObject (readonly)

Since:

  • 0.1.0



36
37
38
# File 'lib/rocket_chat/realtime/client.rb', line 36

def dispatcher
  @dispatcher
end

#driverObject (readonly)

Since:

  • 0.1.0



36
37
38
# File 'lib/rocket_chat/realtime/client.rb', line 36

def driver
  @driver
end

#serverObject (readonly)

Since:

  • 0.1.0



36
37
38
# File 'lib/rocket_chat/realtime/client.rb', line 36

def server
  @server
end

Instance Method Details

#add_to_reactorObject

Add to reactor

Since:

  • 0.1.0



56
57
58
59
60
61
62
63
# File 'lib/rocket_chat/realtime/client.rb', line 56

def add_to_reactor
  monitor = Reactor.register(self)
  return if monitor.nil?

  @adapter = Adapter.new(endpoint, monitor)
  @driver = WebSocket::Driver.client(adapter)
  @dispatcher = Dispatcher.new(self)
end

#connectObject

Connect to server

Since:

  • 0.1.0



68
69
70
71
72
# File 'lib/rocket_chat/realtime/client.rb', line 68

def connect
  add_to_reactor
  driver.start
  driver.text(INITIALIZE_COMMAND.to_json)
end

#disconnectObject

Close connection to server

Since:

  • 0.1.0



77
78
79
80
81
82
# File 'lib/rocket_chat/realtime/client.rb', line 77

def disconnect
  @adapter.dispose
  @dispatcher.dispose
  driver.close
  Reactor.deregister(self)
end

#endpointString

Returns the realtime api endpoint.

Returns:

  • (String)

    the realtime api endpoint

Since:

  • 0.1.0



49
50
51
# File 'lib/rocket_chat/realtime/client.rb', line 49

def endpoint
  "#{server}/websocket"
end

#opened?Boolean

WebSocket is opened

Returns:

  • (Boolean)

    open or not

Since:

  • 0.1.0



89
90
91
# File 'lib/rocket_chat/realtime/client.rb', line 89

def opened?
  driver.state == :open
end

#process(monitor) ⇒ Object

Process I/O

Parameters:

  • monitor (NIO::Monitor)

Since:

  • 0.1.0



98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rocket_chat/realtime/client.rb', line 98

def process(monitor)
  monitor.interests = adapter.pending? ? :rw : :r
  driver.parse(monitor.io.read_nonblock(2**14)) if monitor.readable?
  adapter.pump_buffer if monitor.writeable?
rescue IO::WaitReadable, IO::WaitWritable
  # nope
rescue Errno::ECONNRESET, EOFError, Errno::ECONNABORTED
  RocketChat::Realtime.logger.warn('Remote server is closed.')
  monitor.close
  disconnect
end