-
Notifications
You must be signed in to change notification settings - Fork 1
/
long_poll.rb
142 lines (120 loc) · 3.31 KB
/
long_poll.rb
1
2
3
4
5
6
7
8
9
10
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
56
57
58
59
60
61
62
63
64
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
99
100
101
102
103
104
105
106
107
108
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
136
137
138
139
140
141
142
require 'rubygems'
require 'eventmachine'
require 'uuid'
require 'yajl'
require 'cgi'
class Room < EM::Channel
attr_accessor :log
def initialize
@log = []
super
end
def say(nick, type, text = nil)
m = {:nick => nick, :type => type}
m[:text] = text if text
@log << m
str = Yajl::Encoder.encode({:messages => [m]})
push(str)
end
end
$users = {}
$room = Room.new
$index_html = File.read('index.html')
$client_js = File.read('client.js')
$style_css = File.read('style.css')
class LongPollHttpServer < EM::Connection
def unbind
$room.unsubscribe(@subscription)
end
def receive_data(data)
lines = data.split(/[\r\n]+/)
method, request, version = lines.shift.split(' ', 3)
if request.nil?
puts "#{Time.now} Warning: strange request #{[method, request, version].inspect}"
close_connection
return
else
path, query = request.split('?', 2)
query = CGI.parse(query) if not query.nil?
cookies = {}
lines.each do |line|
if line[0..6].downcase == 'cookie:'
cookies = CGI::Cookie.parse(line.split(':', 2).last.strip)
end
end
end
require "pp"
pp "#{path} with #{query}"
case path
when '/'
respond $index_html, 200, 'text/html'
when '/client.js'
respond $client_js, 200, 'application/javascript'
when '/style.css'
respond $style_css, 200, 'text/css'
when '/join'
nick = query['nick'].first
id = UUID.new.generate
$users[id] = {:nick => nick}
$room.say(nick, "join")
str = Yajl::Encoder.encode({:id => id, :nick => nick})
respond str, 200, 'application/json'
when '/send'
id = query['id'].first
text = query['text'].first
nick = $users[id][:nick]
$room.say nick, 'msg', text.gsub('"', '\"')
str = Yajl::Encoder.encode({})
respond str, 200, 'application/json'
when '/recv'
@subscription = $room.subscribe do |msg|
respond msg, 200, "application/json"
end
# when '/status'
# respond "connections: #{EM.connection_count}\n" +
# "total: #{ObjectSpace.count_objects[:TOTAL]}\n" +
# "free: #{ObjectSpace.count_objects[:FREE]}\n"
when '/who'
str = Yajl::Encoder.encode({:nicks => $users.values.map{|x| x[:nick]}})
respond str, 200, 'application/json'
when '/part'
id = query['id'].first
if $users[id]
nick = $users[id][:nick]
$room.say nick, 'part'
$users.delete id
end
str = Yajl::Encoder.encode({})
respond str, 200, 'application/json'
else
respond "not found", 404
end
end
RESPONSE = [
"HTTP/1.1 %d PLOP",
"Content-length: %d",
"Content-type: %s",
"Connection: close",
"",
"%s"].join("\r\n")
def respond(body, status = 200, content_type = 'text/comet')
send_data RESPONSE % [status, body.length, content_type, body]
close_connection_after_writing
end
end
loop do
begin
GC.start
EM.epoll if EM.epoll?
EM.run do
puts "#{Time.now} Starting server on port #{ARGV.first || 8000}"
EM.start_server '0.0.0.0', ARGV.first || 8000, LongPollHttpServer
end
rescue Interrupt
puts "#{Time.now} Shuting down..."
exit
rescue
puts "#{Time.now} Error: " + $!.message
puts "\t" + $!.backtrace.join("\n\t")
end
end