Class: RocketChat::Realtime::Adapter

Inherits:
Object
  • Object
show all
Defined in:
lib/rocket_chat/realtime/adapter.rb

Overview

Socket Adapter for WebSocket::Driver

Since:

  • 0.1.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url, monitor) ⇒ Adapter

Returns a new instance of Adapter.

Parameters:

  • url (String)

    the server to connect

Since:

  • 0.1.0



15
16
17
18
19
20
# File 'lib/rocket_chat/realtime/adapter.rb', line 15

def initialize(url, monitor)
  @url = url
  @monitor = monitor
  @mutex = Mutex.new
  @buffer = ''
end

Instance Attribute Details

#monitorObject (readonly)

Since:

  • 0.1.0



10
11
12
# File 'lib/rocket_chat/realtime/adapter.rb', line 10

def monitor
  @monitor
end

#urlObject (readonly)

Since:

  • 0.1.0



10
11
12
# File 'lib/rocket_chat/realtime/adapter.rb', line 10

def url
  @url
end

Instance Method Details

#disposeObject

Since:

  • 0.1.0



26
27
28
# File 'lib/rocket_chat/realtime/adapter.rb', line 26

def dispose
  @monitor = nil
end

#pending?Boolean

Returns:

  • (Boolean)

Since:

  • 0.1.0



22
23
24
# File 'lib/rocket_chat/realtime/adapter.rb', line 22

def pending?
  @buffer.empty? == false
end

#pump_bufferNumber

Pump Buffer

Parameters:

  • io (IO)

    the data write to

Returns:

  • (Number)

    total bytes written

Since:

  • 0.1.0



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rocket_chat/realtime/adapter.rb', line 48

def pump_buffer
  @mutex.synchronize do
    written = 0
    written = monitor.io.write_nonblock @buffer unless @buffer.empty?
    @buffer = @buffer.byteslice(written..-1) if written.positive?
    written
  end
rescue IO::WaitWritable, IO::WaitReadable
  written
ensure
  monitor.interests = pending? ? :rw : :r
end

#write(data) ⇒ Object

Enqueue data to send to server

Parameters:

  • data (String)

Since:

  • 0.1.0



35
36
37
38
39
# File 'lib/rocket_chat/realtime/adapter.rb', line 35

def write(data)
  @mutex.synchronize { @buffer = @buffer.dup.concat(data) }
  pump_buffer
  Reactor.selector.wakeup unless monitor.interests == :r
end