-
Notifications
You must be signed in to change notification settings - Fork 1
/
courseboard.rb
51 lines (43 loc) · 921 Bytes
/
courseboard.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
require 'sinatra'
require 'sinatra-websocket'
require 'json'
require 'cgi'
set :server, 'thin'
set :twitter_sockets, []
set :facebook_sockets, []
set :public_folter, 'public'
def connect_to_ws(sockets)
request.websocket do |ws|
ws.onopen do
sockets << ws
end
ws.onmessage do |msg|
EM.next_tick { sockets.each{|s| s.send(msg) } }
end
ws.onclose do
sockets.delete(ws)
end
end
end
def send_message_to_ws(sockets)
data = JSON.parse(request.body.read)
sockets.each do |s|
s.send(CGI.escapeHTML("@#{data['username']}: #{data['text']}"))
end
"Sent"
end
get '/' do
redirect "index.html"
end
get '/twitter' do
connect_to_ws(settings.twitter_sockets)
end
post '/tweet' do
send_message_to_ws(settings.twitter_sockets)
end
get '/facebook' do
connect_to_ws(settings.facebook_sockets)
end
post '/facebook' do
send_message_to_ws(settings.facebook_sockets)
end