Class: Shove::Client::Connection
- Inherits:
-
Object
- Object
- Shove::Client::Connection
- 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
-
#id ⇒ Object
Returns the value of attribute id.
-
#socket ⇒ Object
Returns the value of attribute socket.
-
#url ⇒ Object
Returns the value of attribute url.
Instance Method Summary collapse
- #authorize(app_key = nil) ⇒ Object
-
#channel(name) ⇒ Object
Fetch a channel
namethe name of the channel. -
#connect ⇒ Object
Connect to the shove stream server.
-
#debug(on = true) ⇒ Object
Enable or disable debugging
ontrue to enable debugging. -
#disconnect ⇒ Object
Disconnect form the server.
-
#initialize(app, id) ⇒ Connection
constructor
Create a Publisher
appthe app. -
#on(event, &block) ⇒ Object
Bind to events for a given channel.
- #send_data(data) ⇒ Object
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
#id ⇒ Object
Returns the value of attribute id.
7 8 9 |
# File 'lib/shove/client/connection.rb', line 7 def id @id end |
#socket ⇒ Object
Returns the value of attribute socket.
7 8 9 |
# File 'lib/shove/client/connection.rb', line 7 def socket @socket end |
#url ⇒ Object
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 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
83 84 85 86 87 88 |
# File 'lib/shove/client/connection.rb', line 83 def channel name unless @channels.key?(name) @channels[name] = Channel.new(name, self) end @channels[name] end |
#connect ⇒ Object
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 61 62 |
# File 'lib/shove/client/connection.rb', line 35 def connect 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 => @id until @queue.empty? do send_data @queue.shift end end @socket. do |m, binary| process(Yajl::Parser.parse(m)) 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 |
#disconnect ⇒ Object
Disconnect form the server
65 66 67 68 |
# File 'lib/shove/client/connection.rb', line 65 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
74 75 76 77 78 79 |
# File 'lib/shove/client/connection.rb', line 74 def on event, &block unless @events.key?(event) @events[event] = [] end @events[event] << block end |
#send_data(data) ⇒ Object
102 103 104 105 106 107 108 |
# File 'lib/shove/client/connection.rb', line 102 def send_data data if @connected @socket.(Yajl::Encoder.encode(data)) else @queue << data end end |