Class: Faye::Client
- Inherits:
-
Object
- Object
- Faye::Client
- Extended by:
- Forwardable
- Includes:
- Deferrable, Extensible, Logging, Publisher
- Defined in:
- lib/faye/protocol/client.rb
Constant Summary collapse
- UNCONNECTED =
1
- CONNECTING =
2
- CONNECTED =
3
- DISCONNECTED =
4
- HANDSHAKE =
'handshake'
- RETRY =
'retry'
- NONE =
'none'
- CONNECTION_TIMEOUT =
60.0
Constants included from Logging
Instance Method Summary collapse
-
#connect(&block) ⇒ Object
Request Response MUST include: * channel MUST include: * channel * clientId * successful * connectionType * clientId MAY include: * ext MAY include: * error * id * advice * ext * id * timestamp.
-
#disconnect ⇒ Object
Request Response MUST include: * channel MUST include: * channel * clientId * successful MAY include: * ext * clientId * id MAY include: * error * ext * id.
-
#handshake(&block) ⇒ Object
Request MUST include: * channel * version * supportedConnectionTypes MAY include: * minimumVersion * ext * id.
-
#initialize(endpoint = nil, options = {}) ⇒ Client
constructor
A new instance of Client.
-
#publish(channel, data, options = {}) ⇒ Object
Request Response MUST include: * channel MUST include: * channel * data * successful MAY include: * clientId MAY include: * id * id * error * ext * ext.
-
#subscribe(channel, force = false, &block) ⇒ Object
Request Response MUST include: * channel MUST include: * channel * clientId * successful * subscription * clientId MAY include: * ext * subscription * id MAY include: * error * advice * ext * id * timestamp.
-
#unsubscribe(channel, &block) ⇒ Object
Request Response MUST include: * channel MUST include: * channel * clientId * successful * subscription * clientId MAY include: * ext * subscription * id MAY include: * error * advice * ext * id * timestamp.
Methods included from Extensible
#add_extension, #pipe_through_extensions, #remove_extension
Methods included from Publisher
Methods included from Deferrable
Constructor Details
#initialize(endpoint = nil, options = {}) ⇒ Client
Returns a new instance of Client.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/faye/protocol/client.rb', line 23 def initialize(endpoint = nil, = {}) super() info('New client created for ?', endpoint) @endpoint = endpoint || RackAdapter::DEFAULT_ENDPOINT @channels = Channel::Set.new @dispatcher = Dispatcher.new(self, @endpoint, ) @message_id = 0 @state = UNCONNECTED @response_callbacks = {} @advice = { 'reconnect' => RETRY, 'interval' => 1000.0 * ([:interval] || Engine::INTERVAL), 'timeout' => 1000.0 * ([:timeout] || CONNECTION_TIMEOUT) } @dispatcher.timeout = @advice['timeout'] / 1000.0 @dispatcher.bind(:message, &method(:receive_message)) end |
Instance Method Details
#connect(&block) ⇒ Object
Request Response MUST include: * channel MUST include: * channel
* clientId * successful
* connectionType * clientId
MAY include: * ext MAY include: * error
* id * advice
* ext
* id
* timestamp
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/faye/protocol/client.rb', line 109 def connect(&block) return if @advice['reconnect'] == NONE or @state == DISCONNECTED return handshake { connect(&block) } if @state == UNCONNECTED callback(&block) return unless @state == CONNECTED info('Calling deferred actions for ?', @dispatcher.client_id) set_deferred_status(:succeeded) set_deferred_status(:unknown) return unless @connect_request.nil? @connect_request = true info('Initiating connection for ?', @dispatcher.client_id) ({ 'channel' => Channel::CONNECT, 'clientId' => @dispatcher.client_id, 'connectionType' => @dispatcher.connection_type }, {}) do cycle_connection end end |
#disconnect ⇒ Object
Request Response MUST include: * channel MUST include: * channel
* clientId * successful
MAY include: * ext * clientId
* id MAY include: * error
* ext
* id
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/faye/protocol/client.rb', line 144 def disconnect return unless @state == CONNECTED @state = DISCONNECTED info('Disconnecting ?', @dispatcher.client_id) ({ 'channel' => Channel::DISCONNECT, 'clientId' => @dispatcher.client_id }, {}) do |response| @dispatcher.close if response['successful'] end info('Clearing channel listeners for ?', @dispatcher.client_id) @channels = Channel::Set.new end |
#handshake(&block) ⇒ Object
Request MUST include: * channel
* version
* supportedConnectionTypes
MAY include: * minimumVersion
* ext
* id
Success Response Failed Response MUST include: * channel MUST include: * channel
* version * successful
* supportedConnectionTypes * error
* clientId MAY include: * supportedConnectionTypes
* successful * advice
MAY include: * minimumVersion * version
* advice * minimumVersion
* ext * ext
* id * id
* authSuccessful
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/faye/protocol/client.rb', line 65 def handshake(&block) return if @advice['reconnect'] == NONE return if @state != UNCONNECTED @state = CONNECTING info('Initiating handshake with ?', @endpoint) @dispatcher.select_transport(MANDATORY_CONNECTION_TYPES) ({ 'channel' => Channel::HANDSHAKE, 'version' => BAYEUX_VERSION, 'supportedConnectionTypes' => [@dispatcher.connection_type] }, {}) do |response| if response['successful'] @state = CONNECTED @dispatcher.client_id = response['clientId'] @dispatcher.select_transport(response['supportedConnectionTypes']) info('Handshake successful: ?', @dispatcher.client_id) subscribe(@channels.keys, true) block.call if block_given? else info('Handshake unsuccessful') EventMachine.add_timer(@dispatcher.retry) { handshake(&block) } @state = UNCONNECTED end end end |
#publish(channel, data, options = {}) ⇒ Object
Request Response MUST include: * channel MUST include: * channel
* data * successful
MAY include: * clientId MAY include: * id
* id * error
* ext * ext
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/faye/protocol/client.rb', line 250 def publish(channel, data, = {}) publication = Publication.new connect { info('Client ? queueing published message to ?: ?', @dispatcher.client_id, channel, data) ({ 'channel' => channel, 'data' => data, 'clientId' => @dispatcher.client_id }, ) do |response| if response['successful'] publication.set_deferred_status(:succeeded) else publication.set_deferred_status(:failed, Error.parse(response['error'])) end end } publication end |
#subscribe(channel, force = false, &block) ⇒ Object
Request Response MUST include: * channel MUST include: * channel
* clientId * successful
* subscription * clientId
MAY include: * ext * subscription
* id MAY include: * error
* advice
* ext
* id
* timestamp
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/faye/protocol/client.rb', line 172 def subscribe(channel, force = false, &block) if Array === channel return channel.map { |c| subscribe(c, force, &block) } end subscription = Subscription.new(self, channel, block) has_subscribe = @channels.has_subscription?(channel) if has_subscribe and not force @channels.subscribe([channel], block) subscription.set_deferred_status(:succeeded) return subscription end connect { info('Client ? attempting to subscribe to ?', @dispatcher.client_id, channel) @channels.subscribe([channel], block) unless force ({ 'channel' => Channel::SUBSCRIBE, 'clientId' => @dispatcher.client_id, 'subscription' => channel }, {}) do |response| unless response['successful'] subscription.set_deferred_status(:failed, Error.parse(response['error'])) next @channels.unsubscribe(channel, block) end channels = [response['subscription']].flatten info('Subscription acknowledged for ? to ?', @dispatcher.client_id, channels) subscription.set_deferred_status(:succeeded) end } subscription end |
#unsubscribe(channel, &block) ⇒ Object
Request Response MUST include: * channel MUST include: * channel
* clientId * successful
* subscription * clientId
MAY include: * ext * subscription
* id MAY include: * error
* advice
* ext
* id
* timestamp
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/faye/protocol/client.rb', line 219 def unsubscribe(channel, &block) if Array === channel return channel.map { |c| unsubscribe(c, &block) } end dead = @channels.unsubscribe(channel, block) return unless dead connect { info('Client ? attempting to unsubscribe from ?', @dispatcher.client_id, channel) ({ 'channel' => Channel::UNSUBSCRIBE, 'clientId' => @dispatcher.client_id, 'subscription' => channel }, {}) do |response| next unless response['successful'] channels = [response['subscription']].flatten info('Unsubscription acknowledged for ? from ?', @dispatcher.client_id, channels) end } end |