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::REDIRECT, Protocol::SET_IDENTITY, 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) ⇒ Connection

Create a Publisher app the app



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

def initialize app
  @app = app
  @parser = Yajl::Parser.new(:symbolize_keys => true)
  @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

#auth!Object

helper to auth on all channels if app_key is supplied



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

def auth!
  if @app.app_key
    channel("*").auth @app.channel_key("*")
  end
end

#channel(name) ⇒ Object

Fetch a channel name the name of the channel



90
91
92
93
94
95
# File 'lib/shove/client/connection.rb', line 90

def channel name
  unless @channels.key?(name)
    @channels[name] = Channel.new(name, self)
  end
  @channels[name]
end

#connect(connect_key = nil) ⇒ Object

Connect to the shove stream server



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
61
62
63
64
65
66
67
68
69
# File 'lib/shove/client/connection.rb', line 36

def connect connect_key=nil
  @connect_key = connect_key

  if @connected
    return
  end

  @socket = EM::WebSocketClient.new(url)

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

  @socket.onopen do
    @connected = true
    send_data :opcode => CONNECT, :data => (connect_key || @app.connect_key)
    auth!
    until @queue.empty? do
      send_data @queue.shift
    end
  end

  @socket.onmessage do |m, binary|
    if @debug
      puts "DEBUG RECV #{m}"
    end

    process(Yajl::Parser.parse(m))
  end

end

#debug(on = true) ⇒ Object

Enable or disable debugging on true to enable debugging



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

def debug on=true
  @debug = on
end

#disconnectObject

Disconnect form the server



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

def disconnect
  @forcedc = true
  @socket.unbind
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



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

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

#send_data(data) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/shove/client/connection.rb', line 109

def send_data data
  if @connected
    if @debug
      puts "DEBUG SEND #{data}"
    end
    @socket.send_message(Yajl::Encoder.encode(data))
  else
    if @debug
      puts "DEBUG QUEUE #{data}"
    end
    @queue << data
  end
end