-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.rb
executable file
·99 lines (77 loc) · 2.08 KB
/
server.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
#!/usr/bin/env ruby
require "rubygems"
require "bundler/setup"
require "eventmachine"
require "em-websocket"
require 'uuidtools'
require 'sinatra/base'
require 'thin'
require 'json'
require 'logger'
require 'securerandom'
$logger = Logger.new(File.expand_path('../log/server.log', __FILE__))
def log_info(msg)
$logger.info(msg)
end
class LocationStream
attr_reader :stream_id, :sender, :listeners, :channel
def initialize(ws=nil)
@stream_id = UUIDTools::UUID.random_create.to_s
@sender = nil
@listeners = []
@channel = EM::Channel.new
end
end
class App < Sinatra::Base
def self.streams
(@streams ||= {})
end
get '/' do
send_file File.join(settings.public_folder, 'index.html')
end
get '/view/new' do
s = LocationStream.new
self.class.streams[s.stream_id] = s
redirect("/view/#{s.stream_id}")
end
get '/view/:id' do |id|
trk_link = "http://#{request.host}:4000/send/#{id}"
#TODO: handle stream not found
erb :recv, :locals => {:stream => self.class.streams[id], :tracking_link => trk_link}
end
get '/send/:id' do |id|
#TODO: Handle stream not found
erb :send, :locals => {:stream => self.class.streams[id]}
end
end
EM.run do
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 8080) do |ws|
ws.onopen do
case ws.request['path']
when %r{/view}
if(stream = App.streams[ws.request["query"]["stream_id"]])
stream.channel.subscribe{ |msg| ws.send(msg) }
else
puts "socket not found"
end
when %r{/send}
if(stream = App.streams[ws.request["query"]["stream_id"]])
ws.onmessage do |msg|
stream.channel.push(msg)
end
ws.send("you're a sender")
else
puts "send socket not found"
end
else
log_info "unknown socket path"
end
log_info "WebSocket connection open #{ws.inspect}"
end
# ws.onclose { log_info "Connection closed" }
# ws.onmessage { |msg|
# log_info "Recieved message: #{msg}"
# }
end
Thin::Server.start App, '0.0.0.0', 4000
end