This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.coffee
60 lines (45 loc) · 1.46 KB
/
app.coffee
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
_ = require "underscore"
express = require "express"
http = require "http"
socket = require "socket.io"
app = express()
server = http.createServer app
io = socket.listen server
app.use express.bodyParser()
app.use express.static(__dirname + "/public")
# Sockets code
sockets = []
latestPositionTime = new Date
latestPosition = null
latestTimeout = 4*60*60*1000 # 4 hours..
io.sockets.on "connection", (socket) ->
sockets.push socket
socket.on "disconnect", ->
sockets = _.without sockets, socket
if latestPosition? && (new Date) - latestPositionTime > latestTimeout
latestPosition = null
return unless latestPosition?
socket.emit "position", latestPosition
sendPosition = (position) ->
timestamp = new Date(position.timestamp || position.recorded_at)
return if latestPosition && timestamp < latestPositionTime
latestPositionTime = timestamp
latestPosition = position
_.each sockets, (socket) ->
socket.emit "position", position
clearPosition = ->
latestPositionTime = new Date
latestPosition = null
_.each sockets, (socket) ->
socket.emit "clear"
app.post "/report", (req, res) ->
console.log "got position", req.body
_.defer sendPosition, req.body.location
res.end "Thanks brah!"
app.post "/clear", (req, res) ->
console.log "clearing position", req.body
_.defer clearPosition
res.end "Thanks brah!"
port = Number(process.env.PORT || 5000)
server.listen port, ->
console.log "here we go!"