11
12
13
14
15
16
17
18
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
53
54
55
|
# File 'lib/puggernaut/server/http.rb', line 11
def receive_data(data)
lines = data.split(/[\r\n]+/)
method, request, version = lines.shift.split(' ', 3)
if request.nil?
logger.error "Server::Http#receive_data - Strange request - #{[method, request, version].inspect}"
close_connection
return
else
path, query = request.split('?', 2)
logger.info "Server::Http#receive_data - Request - #{path} - #{query}"
query = CGI.parse(query) if not query.nil?
end
puts path
if path == '/'
channels, @join_leave, lasts, time, user_id = query_defaults(query)
unless channels.empty?
@channel = Channel.create(channels, user_id)
if @join_leave && user_id
join_channels(channels, user_id)
end
messages = gather_messages(channels, lasts, time)
unless messages.empty?
respond messages
else
EM::Timer.new(30) { respond }
logger.info "Server::Http#receive_data - Subscribed - #{@channel.channels.join(", ")}"
@subscription_id = @channel.subscribe { |str| respond str }
end
else
respond "no channel specified", 500
end
elsif path == '//inhabitants'
channels = query['channel'].dup rescue []
user_ids = channels.collect do |c|
Channel.inhabitants(c)
end
respond user_ids.flatten.uniq.join('|')
else
respond "not found", 404
end
end
|