-
Notifications
You must be signed in to change notification settings - Fork 4
/
server.coffee
77 lines (65 loc) · 2.05 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
http: require "http"
url: require "url"
fs: require "fs"
io: require "./vendor/Socket.IO-node/lib/socket.io"
sys: require "sys"
tojson: JSON.stringify
fromjson: JSON.parse
send404: (res) ->
res.writeHead 404
res.write '404'
res.end()
server: http.createServer (req, res) ->
path: (url.parse req.url).pathname;
if path == "/" then path: "/index.html"
filepath: __dirname + "/client" + path
sys.puts filepath
try
res.writeHead 200, {'Content-Type': 'text/' + if path.substr(-3) == '.js' then 'js' else 'html'}
res.write fs.readFileSync(filepath, 'utf8'), 'utf8'
res.end()
sys.puts "200: " + filepath
catch err
sys.puts "404: " + filepath
send404 res
server.listen parseInt(process.argv[2] or "80", 10)
all_clients: []
find_partner: (client) ->
if not client.room?
for otherclient in all_clients when otherclient isnt client
if not otherclient.room?
put_into_room client, otherclient
break
setTimeout (find_partner <- null, client), 200
put_into_room: (clients...) ->
for client in clients
client.room: clients
client.send tojson {"type": "connect"}
client.send_to_others: send_to_others <- null, client
send_to_others: (client, message) ->
for otherclient in client.room when otherclient isnt client
otherclient.send message
disconnect: (client) ->
if client.room?
for c in client.room
c.send tojson {"type": "disconnect"}
c.room: undefined
io.listen server, {
onClientConnect: (client) ->
all_clients.push client
onClientMessage: (message, client) ->
sys.puts message
msg: fromjson message
switch msg.type
when "message"
if client.room?
client.send tojson {"type": "message", "you": true, "msg": msg.msg}
client.send_to_others tojson {"type": "message", "you": false, "msg": msg.msg}
when "wantdisconnect"
disconnect client
when "wantpartner"
find_partner client
onClientDisconnect: (client) ->
disconnect client
all_clients: c for c in all_clients when c isnt client
}