Class: Shove::Client::Channel

Inherits:
Object
  • Object
show all
Includes:
Protocol
Defined in:
lib/shove/client/channel.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(name, conn) ⇒ Channel

Create a new channel name The name of the channel



11
12
13
14
15
16
# File 'lib/shove/client/channel.rb', line 11

def initialize name, conn
  @conn = conn
  @name = name.to_s
  @callbacks = {}
  @subscribe_sent = false
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

Instance Method Details

#auth(channel_key) ⇒ Object



85
86
87
# File 'lib/shove/client/channel.rb', line 85

def auth channel_key
  authorize channel_key
end

#authorize(channel_key) ⇒ Object

authorize pub/sub on this channel



81
82
83
# File 'lib/shove/client/channel.rb', line 81

def authorize channel_key
  @conn.send_data :opcode => AUTHORIZE, :channel => @name, :data => channel_key.to_s
end

#on(event, &block) ⇒ Object

Bind a block to an event event the event name block the callback



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/shove/client/channel.rb', line 21

def on event, &block
  unless @callbacks.has_key?(event)
    @callbacks[event] = []
  end
  result = Callback.new(@callbacks[event], block)
  @callbacks[event] << result

  if @name != "direct" && !@subscribe_sent
    subscribe
  end

  result
end

#process(message) ⇒ Object

Process a message for the channel message the message in question



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
# File 'lib/shove/client/channel.rb', line 37

def process message
  op = message["opcode"]
  data = message["data"]

  case op
  when PUBLISH
    emit("message", data)
  when SUBSCRIBE_GRANTED
    emit("subscribe")
  when SUBSCRIBE_DENIED
    emit("subscribe_denied")
  when PUBLISH_DENIED
    emit("publish_denied")
  when PUBLISH_GRANTED
    emit("publish_granted")
  when UNSUBSCRIBE_COMPLETE
    emit("unsubscribe")
    @callbacks.clear
  when AUTHORIZE_DENIED
    emit("authorize_denied")
  when AUTHORIZE_COMPLETE
    emit("authorize_complete")
  end

end

#publish(msg) ⇒ Object

publish a message on the channel msg the message to publish. It must implement to_s



65
66
67
# File 'lib/shove/client/channel.rb', line 65

def publish msg
  @conn.send_data :opcode => PUBLISH, :channel => @name, :data => msg.to_s
end

#subscribeObject

subscribe to the channel, by sending to the remote



70
71
72
73
# File 'lib/shove/client/channel.rb', line 70

def subscribe
  @conn.send_data :opcode => SUBSCRIBE, :channel => @name
  @subscribe_sent = true
end

#unsubscribeObject

unsubscribe from the channel



76
77
78
# File 'lib/shove/client/channel.rb', line 76

def unsubscribe
  @conn.send_data :opcode => UNSUBSCRIBE, :channel => @name
end