Class: Faye::Server

Inherits:
Object
  • Object
show all
Includes:
Extensible, Logging
Defined in:
lib/faye/protocol/server.rb

Constant Summary

Constants included from Logging

Logging::DEFAULT_LOG_LEVEL, Logging::LOG_LEVELS

Instance Attribute Summary

Attributes included from Logging

#log_level

Instance Method Summary collapse

Methods included from Extensible

#add_extension, #pipe_through_extensions, #remove_extension

Methods included from Logging

#log

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



7
8
9
10
11
12
13
# File 'lib/faye/protocol/server.rb', line 7

def initialize(options = {})
  info('New server created')
  @options     = options
  @channels    = Channel::Tree.new
  @connections = {}
  @namespace   = Namespace.new
end

Instance Method Details

#client_idsObject



15
16
17
# File 'lib/faye/protocol/server.rb', line 15

def client_ids
  @connections.keys
end

#flush_connection(messages) ⇒ Object



54
55
56
57
58
59
# File 'lib/faye/protocol/server.rb', line 54

def flush_connection(messages)
  [messages].flatten.each do |message|
    connection = @connections[message['clientId']]
    connection.flush! if connection
  end
end

#process(messages, local_or_remote = false, &callback) ⇒ Object



19
20
21
22
23
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
# File 'lib/faye/protocol/server.rb', line 19

def process(messages, local_or_remote = false, &callback)
  socket = local_or_remote.is_a?(WebSocket) ? local_or_remote : nil
  local  = (local_or_remote == true)
  
  debug('Processing messages from ? client', local ? 'LOCAL' : 'REMOTE')
  
  messages = [messages].flatten
  processed, responses = 0, []
  
  gather_replies = lambda do |replies|
    responses.concat(replies)
    processed += 1
    callback.call(responses.compact) if processed == messages.size
  end
  
  handle_reply = lambda do |replies|
    extended, expected = 0, replies.size
    gather_replies.call(replies) if expected == 0
    
    replies.each_with_index do |reply, i|
      pipe_through_extensions(:outgoing, reply) do |message|
        replies[i] = message
        extended  += 1
        gather_replies.call(replies) if extended == expected
      end
    end
  end
  
  messages.each do |message|
    pipe_through_extensions(:incoming, message) do |piped_message|
      handle(piped_message, socket, local, &handle_reply)
    end
  end
end