Class: Faye::Server
- Inherits:
-
Object
- Object
- Faye::Server
- Defined in:
- lib/faye/server.rb
Instance Method Summary collapse
- #client_ids ⇒ Object
-
#connect(message, local = false) ⇒ Object
MUST contain * clientId * connectionType MAY contain * ext * id.
-
#disconnect(message, local = false) ⇒ Object
MUST contain * clientId MAY contain * ext * id.
- #handle(message, local = false, &callback) ⇒ Object
-
#handshake(message, local = false) ⇒ Object
MUST contain * version * supportedConnectionTypes MAY contain * minimumVersion * ext * id.
-
#initialize(options = {}) ⇒ Server
constructor
A new instance of Server.
- #process(messages, local = false, &callback) ⇒ Object
-
#subscribe(message, local = false) ⇒ Object
MUST contain * clientId * subscription MAY contain * ext * id.
-
#unsubscribe(message, local = false) ⇒ Object
MUST contain * clientId * subscription MAY contain * ext * id.
-
#update(message, client) ⇒ Object
Notifies the server of stale connections that should be deleted.
Constructor Details
Instance Method Details
#client_ids ⇒ Object
205 206 207 |
# File 'lib/faye/server.rb', line 205 def client_ids @clients.keys end |
#connect(message, local = false) ⇒ Object
MUST contain * clientId
* connectionType
MAY contain * ext
* id
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/faye/server.rb', line 87 def connect(, local = false) response = { 'channel' => Channel::CONNECT, 'id' => ['id'] } client_id = ['clientId'] client = client_id ? @clients[client_id] : nil response['error'] = Error.client_unknown(client_id) if client.nil? response['error'] = Error.parameter_missing('clientId') if client_id.nil? response['error'] = Error.parameter_missing('connectionType') if ['connectionType'].nil? response['successful'] = response['error'].nil? return response unless response['successful'] response['clientId'] = client.id response end |
#disconnect(message, local = false) ⇒ Object
MUST contain * clientId MAY contain * ext
* id
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/faye/server.rb', line 108 def disconnect(, local = false) response = { 'channel' => Channel::DISCONNECT, 'id' => ['id'] } client_id = ['clientId'] client = client_id ? @clients[client_id] : nil response['error'] = Error.client_unknown(client_id) if client.nil? response['error'] = Error.parameter_missing('clientId') if client_id.nil? response['successful'] = response['error'].nil? return response unless response['successful'] destroy_client(client) response['clientId'] = client_id response end |
#handle(message, local = false, &callback) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/faye/server.rb', line 24 def handle(, local = false, &callback) client_id = ['clientId'] channel = ['channel'] if Channel.(channel) response = __send__(Channel.parse(channel)[1], , local) client_id ||= response['clientId'] response['advice'] ||= {} response['advice']['reconnect'] ||= @clients.has_key?(client_id) ? 'retry' : 'handshake' response['advice']['interval'] ||= Connection::INTERVAL * 1000 response['id'] = ['id'] return callback[response] unless response['channel'] == Channel::CONNECT and response['successful'] == true return connection(response['clientId']).connect do |events| callback[[response] + events] end end return callback[[]] if ['clientId'].nil? or Channel.service?(channel) @channels.glob(channel).each { |c| c << } callback[ { 'channel' => channel, 'successful' => true, 'id' => ['id'] } ] end |
#handshake(message, local = false) ⇒ Object
MUST contain * version
* supportedConnectionTypes
MAY contain * minimumVersion
* ext
* id
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/faye/server.rb', line 60 def handshake(, local = false) response = { 'channel' => Channel::HANDSHAKE, 'version' => BAYEUX_VERSION, 'supportedConnectionTypes' => CONNECTION_TYPES, 'id' => ['id'] } response['error'] = Error.parameter_missing('version') if ['version'].nil? client_conns = ['supportedConnectionTypes'] if client_conns common_conns = client_conns.select { |c| CONNECTION_TYPES.include?(c) } response['error'] = Error.conntype_mismatch(*client_conns) if common_conns.empty? else response['error'] = Error.parameter_missing('supportedConnectionTypes') end response['successful'] = response['error'].nil? return response unless response['successful'] response['clientId'] = generate_id response end |
#process(messages, local = false, &callback) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/faye/server.rb', line 10 def process(, local = false, &callback) = [].flatten processed, responses = 0, [] .each do || handle(, local) do |reply| reply = [reply].flatten responses.concat(reply) processed += 1 callback[responses] if processed == .size end end end |
#subscribe(message, local = false) ⇒ Object
MUST contain * clientId
* subscription
MAY contain * ext
* id
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/faye/server.rb', line 131 def subscribe(, local = false) response = { 'channel' => Channel::SUBSCRIBE, 'clientId' => ['clientId'], 'id' => ['id'] } client_id = ['clientId'] client = client_id ? @clients[client_id] : nil subscription = ['subscription'] subscription = [subscription].flatten response['error'] = Error.client_unknown(client_id) if client.nil? response['error'] = Error.parameter_missing('clientId') if client_id.nil? response['error'] = Error.parameter_missing('subscription') if ['subscription'].nil? response['subscription'] = subscription.compact subscription.each do |channel| next if response['error'] response['error'] = Error.channel_forbidden(channel) unless Channel.subscribable?(channel) response['error'] = Error.channel_invalid(channel) unless Channel.valid?(channel) next if response['error'] channel = @channels[channel] ||= Channel.new(channel) client.subscribe(channel) end response['successful'] = response['error'].nil? response end |
#unsubscribe(message, local = false) ⇒ Object
MUST contain * clientId
* subscription
MAY contain * ext
* id
166 167 168 169 170 171 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 |
# File 'lib/faye/server.rb', line 166 def unsubscribe(, local = false) response = { 'channel' => Channel::UNSUBSCRIBE, 'clientId' => ['clientId'], 'id' => ['id'] } client_id = ['clientId'] client = client_id ? @clients[client_id] : nil subscription = ['subscription'] subscription = [subscription].flatten response['error'] = Error.client_unknown(client_id) if client.nil? response['error'] = Error.parameter_missing('clientId') if client_id.nil? response['error'] = Error.parameter_missing('subscription') if ['subscription'].nil? response['subscription'] = subscription.compact subscription.each do |channel| next if response['error'] if not Channel.valid?(channel) response['error'] = Error.channel_invalid(channel) next end channel = @channels[channel] client.unsubscribe(channel) if channel end response['successful'] = response['error'].nil? response end |
#update(message, client) ⇒ Object
Notifies the server of stale connections that should be deleted
200 201 202 203 |
# File 'lib/faye/server.rb', line 200 def update(, client) return unless == :stale_client destroy_client(client) end |