Module: DuoRuby::Socket::Transport

Included in:
DuoRuby::Socket
Defined in:
lib/duoruby/socket/transport.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(receiver) ⇒ Object



6
7
8
# File 'lib/duoruby/socket/transport.rb', line 6

def self.included(receiver)
  receiver.extend(ClassMethods)
end

Instance Method Details

#connect(url: nil, path: "/duoruby/socket", reconnect: false, backoff: 1) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/duoruby/socket/transport.rb', line 10

def connect(url: nil, path: "/duoruby/socket", reconnect: false, backoff: 1)
  raise "already connected" if @socket

  @connect_url = url || self.class.default_socket_url(path)
  @reconnect = reconnect
  @reconnect_backoff = backoff
  open_socket
  self
end

#open_socketObject



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/duoruby/socket/transport.rb', line 27

def open_socket
  @socket = self.class.socket_class.new(@connect_url)
  @transport = proc { |message| socket.write(JSON.generate(message)) }

  socket.on(:open) { trigger(:$connect) }
  socket.on(:message) { |event| receive(JSON.parse(event.data)) }
  socket.on(:close) do
    trigger(:$disconnect)
    cancel_pending_calls
    schedule_reconnect if @reconnect
  end
end

#reconnectObject



20
21
22
23
24
25
# File 'lib/duoruby/socket/transport.rb', line 20

def reconnect
  @socket = nil
  open_socket
  trigger(:$reconnect)
  self
end