Class: Shove::Client::Connection

Inherits:
Object
  • Object
show all
Includes:
Protocol
Defined in:
lib/shove/client/connection.rb

Constant Summary

Constants included from Protocol

Protocol::AUTHORIZE, Protocol::AUTHORIZE_COMPLETE, Protocol::AUTHORIZE_DENIED, Protocol::CONNECT, Protocol::CONNECT_DENIED, Protocol::CONNECT_GRANTED, Protocol::DENY_CONNECT, Protocol::DENY_CONTROL, Protocol::DENY_PUBLISH, Protocol::DENY_SUBSCRIBE, Protocol::DISCONNECT, Protocol::DISCONNECT_COMPLETE, Protocol::ERROR, Protocol::GRANT_CONNECT, Protocol::GRANT_CONTROL, Protocol::GRANT_PUBLISH, Protocol::GRANT_SUBSCRIBE, Protocol::LOG, Protocol::LOG_DENIED, Protocol::LOG_STARTED, Protocol::PRESENCE_LIST, Protocol::PRESENCE_SUBSCRIBED, Protocol::PRESENCE_UNSUBSCRIBED, Protocol::PUBLISH, Protocol::PUBLISH_DENIED, Protocol::PUBLISH_GRANTED, Protocol::SUBSCRIBE, Protocol::SUBSCRIBE_DENIED, Protocol::SUBSCRIBE_GRANTED, Protocol::UNSUBSCRIBE, Protocol::UNSUBSCRIBE_COMPLETE

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, id) ⇒ Connection

Create a Publisher app the app



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/shove/client/connection.rb', line 11

def initialize app, id
  @id = id
  @app = app
  @parser = Yajl::Parser.new(:symbolize_keys => true)
  @config = app.config
  @hosts = app.hosts
  @channels = {}
  @events = {}
  @queue = []
  @forcedc = false
  @connected = false
end

Instance Attribute Details

#idObject

Returns the value of attribute id.



7
8
9
# File 'lib/shove/client/connection.rb', line 7

def id
  @id
end

#socketObject

Returns the value of attribute socket.



7
8
9
# File 'lib/shove/client/connection.rb', line 7

def socket
  @socket
end

#urlObject

Returns the value of attribute url.



7
8
9
# File 'lib/shove/client/connection.rb', line 7

def url
  @url
end

Instance Method Details

#authorize(app_key = nil) ⇒ Object



24
25
26
# File 'lib/shove/client/connection.rb', line 24

def authorize app_key=nil
  send_data :opcode => AUTHORIZE, :data => (app_key || @config.app_key)
end

#channel(name) ⇒ Object

Fetch a channel name the name of the channel



81
82
83
84
85
86
87
88
89
# File 'lib/shove/client/connection.rb', line 81

def channel name
  unless @channels.key?(name)
    @channels[name] = Channel.new(name, self)
    if name != "direct"
      @channels[name].subscribe
    end
  end
  @channels[name]
end

#connectObject

Connect to the shove stream server



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/shove/client/connection.rb', line 35

def connect

  if @connected
    return
  end

  @socket = EM::WebSocketClient.new(url)
  @socket.onopen do
    @connected = true
    send_data :opcode => CONNECT, :data => @id
    until @queue.empty? do
      send_data @queue.shift
    end
  end
  
  @socket.onmessage do |m|
    process Yajl::Parser.parse(m)
  end

  @socket.onclose do
    @connected = false
    unless @forcedc
      reconnect
    end
  end
end

#debug(on = true) ⇒ Object

Enable or disable debugging on true to enable debugging



30
31
32
# File 'lib/shove/client/connection.rb', line 30

def debug on=true
  @debug = on
end

#disconnectObject

Disconnect form the server



63
64
65
66
# File 'lib/shove/client/connection.rb', line 63

def disconnect
  @forcedc = true
  @socket.disconnect
end

#on(event, &block) ⇒ Object

Bind to events for a given channel. channel the channel name to subscribe to event the event name to bind to block the block which is called when a message is received



72
73
74
75
76
77
# File 'lib/shove/client/connection.rb', line 72

def on event, &block
  unless @events.key?(event)
    @events[event] = []
  end
  @events[event] << block
end

#send_data(data) ⇒ Object



103
104
105
106
107
108
109
# File 'lib/shove/client/connection.rb', line 103

def send_data data
  if @connected
    @socket.send_data(Yajl::Encoder.encode(data))
  else
    @queue << data
  end
end