Class: WebSocketHook

Inherits:
Object
  • Object
show all
Defined in:
lib/websockethook.rb

Constant Summary collapse

DEFAULT_HOST =
'ws://websockethook.io'
DEFAULT_SLEEP =
0.1
DEFAULT_KEEP_ALIVE =
true
DEFAULT_PING =
20

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ WebSocketHook

Returns a new instance of WebSocketHook.



11
12
13
14
15
16
17
# File 'lib/websockethook.rb', line 11

def initialize(options = {})
  @stopping = false
  initialize_host options
  initialize_pause options
  initialize_keep_alive options
  initialize_ping options
end

Instance Method Details

#listen(id = nil, &block) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/websockethook.rb', line 19

def listen(id=nil, &block)
  fail 'Block must be provided' unless block && block.is_a?(Proc)
  begin
    @stopping = false
    listener(id, &block)
    restart = @keep_alive && !@stopping
    if restart
      block.call type: 'restart', message: 'restarting connection since it was lost'
      sleep 5
    end
  end while restart
end

#stopObject



32
33
34
35
# File 'lib/websockethook.rb', line 32

def stop
  @stopping = true
  stop_em
end