-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.rb
49 lines (44 loc) · 1.08 KB
/
app.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
require 'rubygems'
require 'em-websocket'
require 'redis'
require 'geocoder'
require 'json'
require 'webrick'
$channel = EM::Channel.new
SOCKETS = []
Thread.new do
EventMachine.run do
EventMachine::WebSocket.start(:host => '0.0.0.0', :port => 8181) do |ws|
ws.onopen do
puts "creating sockets"
$channel.subscribe { |msg| ws.send msg }
SOCKETS << ws
end
ws.onclose do
puts "deleting socket"
$channel.unsubscribe(sid)
SOCKETS.delete ws
end
end
end
end
Thread.new do
Geocoder.configure(:lookup => :geocoder_ca, :cache => Redis.new)
redis = Redis.new
redis.subscribe('mapdata') do |on|
on.message do |channel, msg|
begin
data = JSON.parse(msg)
geocoded = Geocoder.search(data["address"])
data["coords"] = [geocoded[0].longitude, geocoded[0].latitude]
msg = JSON.generate(data)
puts "##{channel} -> #{msg}"
$channel.push msg
rescue => e
#Just report the issue and move on
puts "[RESCUED]: #{e.message}"
end
end
end
end
sleep