-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.coffee
151 lines (119 loc) · 3.7 KB
/
server.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
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
143
144
145
146
147
148
149
HOST: null
PORT: process.env.PORT || 8001
MESSAGE_BACKLOG: 200
SESSION_TIMEOUT: 60 * 1000
starttime: new Date().getTime()
mem: process.memoryUsage()
setInterval( ->
mem: process.memoryUsage()
10*1000)
fu: require "./fu"
sys: require "sys"
url: require "url"
qs: require "querystring"
sessions: {}
class Channel
messages: []
callbacks: []
appendMessage: (nick, type, text) ->
m: { nick: nick, type: type, text: text, timestamp: new Date().getTime() }
switch type
when "msg"
sys.puts "<" + nick + ">"
when "join"
sys.puts nick + " join"
when "part"
sys.puts nick + " part"
@messages.push m
@callbacks.shift().callback([m]) while @callbacks.length > 0
@messages.shift() while @messages.length > MESSAGE_BACKLOG
query: (since, callback) ->
matching: []
for message in @messages
matching.push message if message.timestamp > since
if matching.length isnt 0
callback matching
else
@callbacks.push { timestamp: new Date(), callback: callback }
setInterval( =>
now: new Date()
@callbacks.shift().callback([]) while @callbacks.length > 0 and now - @callbacks[0].timestamp > 30*1000
3000)
channel: new Channel()
createSession: (nick) ->
if nick.length > 50 then return null
if /[^\w_\-^!]/.exec(nick) then return null
for session in sessions
if session.nick == nick then return null
session: {
nick: nick,
id: Math.floor(Math.random()*99999999999).toString(),
timestamp: new Date(),
poke: ->
session.timestamp: new Date()
destroy: ->
channel.appendMessage session.nick, "part"
delete sessions[session.id]
}
sessions[session.id]: session
session
kill_sessions: ->
now: new Date()
for session in sessions
if !sessions.hasOwnProperty(id) then continue
session: session[id]
if now - session.timestamp > SESSION_TIMEOUT then session.destroy()
setInterval kill_sessions, 1000
fu.listen(PORT, HOST)
fu.get("/", fu.staticHandler("index.html"))
fu.get("/style.css", fu.staticHandler("style.css"))
fu.get("/client.js", fu.staticHandler("client.js"))
fu.get("/jquery-1.4.2.min.js", fu.staticHandler("jquery-1.4.2.min.js"))
fu.get('/who', (req, res) ->
nicks: []
for session in sessions
if !sessions.hasOwnProperty(id) then continue
nicks.push(session.nick)
res.simpleJSON(200, { nicks: nicks, rss: mem.rss })
)
fu.get('/join', (req, res) ->
nick: qs.parse(url.parse(req.url).query).nick
if nick.length == 0
res.simpleJSON(400, { error: "Bad nick." })
return
session: createSession(nick)
if session == null
res.simpleJSON(400, { error: "Nick in use." })
return
channel.appendMessage(session.nick, "join")
res.simpleJSON(200, { id: session.id, nick: session.nick, rss: mem.rss, starttime: starttime })
)
fu.get("/send", (req, res) ->
id: qs.parse(url.parse(req.url).query).id
text: qs.parse(url.parse(req.url).query).text
session: sessions[id]
if !session or !text
res.simpleJSON(400, { error: "No such session id"})
return
session.poke()
channel.appendMessage session.nick, "msg", text
res.simpleJSON(200, { rss: mem.rss })
)
fu.get("/part", (req, res) ->
id: qs.parse(url.parse(req.url).query).id
if id and sessions[id]
session: sessions[id]
session.destroy()
res.simpleJSON(200, { rss: mem.rss })
)
fu.get("/recv", (req, res) ->
id: qs.parse(url.parse(req.url).query).id
if id and sessions[id]
session: sessions[id]
session.poke()
since: parseInt(qs.parse(url.parse(req.url).query).since, 10)
channel.query(since, (messages) ->
if session then session.poke()
res.simpleJSON(200, { messages: messages, rss: mem.rss })
)
)