Class: Sinatra::WebSocketIO::Client

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/sinatra/websocketio/client.rb

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ Client

Returns a new instance of Client.



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/sinatra/websocketio/client.rb', line 17

def initialize(url)
  @url = url
  @session = nil
  @websocket = nil
  @connecting = false
  @running = false
  @timeout = 5

  on :__session_id do |session_id|
    @session = session_id
    emit :connect, @session
  end

  on :connect do
    @thread_heartbeat = Thread.new do
      while @connecting
        push :__heartbeat, {:time => Time.now.to_i}
        sleep 5
      end
    end
  end
end

Instance Attribute Details

#connectingObject

Returns the value of attribute connecting.



15
16
17
# File 'lib/sinatra/websocketio/client.rb', line 15

def connecting
  @connecting
end

#runningObject

Returns the value of attribute running.



15
16
17
# File 'lib/sinatra/websocketio/client.rb', line 15

def running
  @running
end

#sessionObject (readonly)

Returns the value of attribute session.



14
15
16
# File 'lib/sinatra/websocketio/client.rb', line 14

def session
  @session
end

#timeoutObject

Returns the value of attribute timeout.



15
16
17
# File 'lib/sinatra/websocketio/client.rb', line 15

def timeout
  @timeout
end

#urlObject (readonly)

Returns the value of attribute url.



14
15
16
# File 'lib/sinatra/websocketio/client.rb', line 14

def url
  @url
end

Instance Method Details

#closeObject



86
87
88
89
# File 'lib/sinatra/websocketio/client.rb', line 86

def close
  @running = false
  @websocket.close
end

#connectObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/sinatra/websocketio/client.rb', line 40

def connect
  return self if connecting
  this = self
  @running = true
  url = @session ? "#{@url}/session=#{@session}" : @url
  @websocket = nil
  begin
    Timeout::timeout @timeout do
      @websocket = WebSocket::Client::Simple::Client.new url
    end
  rescue StandardError, Timeout::Error => e
    this.emit :error, e
    Thread.new do
    sleep 5
      connect
    end
  end
  return self unless @websocket

  @websocket.on :message do |msg|
    begin
      data = JSON.parse msg.data
      this.emit data['type'], data['data']
    rescue => e
      this.emit :error, e
    end
  end

  @websocket.on :close do |e|
    if this.connecting
      this.connecting = false
      this.emit :disconnect, e
    end
    if this.running
      sleep 1
      this.connect
    end
  end

  @websocket.on :open do
    this.connecting = true
  end

  return self
end

#push(type, data) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/sinatra/websocketio/client.rb', line 91

def push(type, data)
  if !@connecting
    emit :error, 'websocket not connecting'
    return
  end
  begin
    Timeout::timeout @timeout do
      @websocket.send({:type => type, :data => data, :session => @session}.to_json)
    end
  rescue Timeout::Error, StandardError => e
    emit :error, e
  end
end