Class: Faye::Engine::Proxy
- Inherits:
-
Object
- Object
- Faye::Engine::Proxy
- Extended by:
- Forwardable
- Defined in:
- lib/faye/engines/proxy.rb
Constant Summary
Constants included from Logging
Instance Attribute Summary collapse
-
#interval ⇒ Object
readonly
Returns the value of attribute interval.
-
#timeout ⇒ Object
readonly
Returns the value of attribute timeout.
Instance Method Summary collapse
- #close ⇒ Object
- #close_connection(client_id) ⇒ Object
- #connect(client_id, options = {}, &callback) ⇒ Object
- #connection(client_id, create) ⇒ Object
- #deliver(client_id, messages) ⇒ Object
- #disconnect ⇒ Object
- #flush_connection(client_id, close = true) ⇒ Object
- #generate_id ⇒ Object
- #has_connection?(client_id) ⇒ Boolean
-
#initialize(options) ⇒ Proxy
constructor
A new instance of Proxy.
- #open_socket(client_id, socket) ⇒ Object
- #publish(message) ⇒ Object
Methods included from Publisher
Constructor Details
#initialize(options) ⇒ Proxy
Returns a new instance of Proxy.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/faye/engines/proxy.rb', line 39 def initialize() super() @options = @connections = {} @interval = @options[:interval] || INTERVAL @timeout = @options[:timeout] || TIMEOUT engine_class = @options[:type] || Memory @engine = engine_class.create(self, @options) bind :close do |client_id| EventMachine.next_tick { flush_connection(client_id) } end debug('Created new engine: ?', @options) end |
Instance Attribute Details
#interval ⇒ Object (readonly)
Returns the value of attribute interval.
34 35 36 |
# File 'lib/faye/engines/proxy.rb', line 34 def interval @interval end |
#timeout ⇒ Object (readonly)
Returns the value of attribute timeout.
34 35 36 |
# File 'lib/faye/engines/proxy.rb', line 34 def timeout @timeout end |
Instance Method Details
#close ⇒ Object
110 111 112 113 |
# File 'lib/faye/engines/proxy.rb', line 110 def close @connections.keys.each { |client_id| flush_connection(client_id) } @engine.disconnect end |
#close_connection(client_id) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/faye/engines/proxy.rb', line 77 def close_connection(client_id) debug('Closing connection for ?', client_id) return unless conn = @connections[client_id] conn.socket.close if conn.socket trigger('connection:close', client_id) @connections.delete(client_id) end |
#connect(client_id, options = {}, &callback) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/faye/engines/proxy.rb', line 57 def connect(client_id, = {}, &callback) debug('Accepting connection from ?', client_id) @engine.ping(client_id) conn = connection(client_id, true) conn.connect(, &callback) @engine.empty_queue(client_id) end |
#connection(client_id, create) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/faye/engines/proxy.rb', line 69 def connection(client_id, create) conn = @connections[client_id] return conn if conn or not create @connections[client_id] = Connection.new(self, client_id) trigger('connection:open', client_id) @connections[client_id] end |
#deliver(client_id, messages) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/faye/engines/proxy.rb', line 90 def deliver(client_id, ) return if ! || .empty? return false unless conn = connection(client_id, false) .each(&conn.method(:deliver)) true end |
#disconnect ⇒ Object
115 116 117 |
# File 'lib/faye/engines/proxy.rb', line 115 def disconnect @engine.disconnect if @engine.respond_to?(:disconnect) end |
#flush_connection(client_id, close = true) ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/faye/engines/proxy.rb', line 101 def flush_connection(client_id, close = true) return unless client_id debug('Flushing connection for ?', client_id) return unless conn = connection(client_id, false) conn.socket = nil unless close conn.flush close_connection(client_id) end |
#generate_id ⇒ Object
97 98 99 |
# File 'lib/faye/engines/proxy.rb', line 97 def generate_id Engine.random end |
#has_connection?(client_id) ⇒ Boolean
65 66 67 |
# File 'lib/faye/engines/proxy.rb', line 65 def has_connection?(client_id) @connections.has_key?(client_id) end |
#open_socket(client_id, socket) ⇒ Object
85 86 87 88 |
# File 'lib/faye/engines/proxy.rb', line 85 def open_socket(client_id, socket) conn = connection(client_id, true) conn.socket = socket end |
#publish(message) ⇒ Object
119 120 121 122 |
# File 'lib/faye/engines/proxy.rb', line 119 def publish() channels = Channel.(['channel']) @engine.publish(, channels) end |