-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
89 lines (81 loc) · 2.17 KB
/
app.js
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
/*
* filename: app.js
* author: firekyrin
* uri: http://www.firekyrin.com/
* version: 1.0
*/
var express = require('express');
//var app = express.createServer();
var http = require('http');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
app.configure(function () {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.logger());
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: "skjghskdjfhbqigohqdioukd", }));
});
var conns = {};
function getUserList()
{
var ret = {};
for(var ccid in conns)
{
/*
ret[ccid]['ccid'] = conns[ccid].cid;
ret[ccid]['cnick'] = conns[ccid].cnick;
*/
ret[ccid]= conns[ccid].cnick;
}
return ret;
}
io.sockets.on('connection', function (socket) {
//var cid = socket.id;
var currentClient = {
cid: socket.id,
cnick: "aaa",
socket: socket
};
//conns[cid] = socket;
conns[currentClient.cid] = currentClient;
for(var ccid in conns) {
var soc = conns[ccid].socket;
soc.emit('join', {cid: conns[ccid]['cid']});
soc.emit('userlist', JSON.stringify(getUserList()));
}
socket.on('disconnect', function () {
delete conns[currentClient.cid];
for(var ccid in conns) {
var soc = conns[ccid].socket;
soc.emit('quit', {cid: conns[ccid].cid});
soc.emit('userlist', JSON.stringify(getUserList()));
}
});
socket.on('say', function (data) {
data.cid = currentClient.cid;
for(var ccid in conns) {
var soc = conns[ccid].socket;
soc.emit('broadcast', data);
}
});
socket.on('submitnick', function (data) {
//console.log('my server get cid:'+data.cid);
console.log('my server get nick:'+data.myname);
conns[currentClient.cid].cnick = data.myname;
for(var ccid in conns) {
var soc = conns[ccid].socket;
soc.emit('userlist', JSON.stringify(getUserList()));
}
});
});
app.get('/', function (req, res) {
//res.sendfile(__dirname + '/public/index.html');
res.sendfile(__dirname + '/index.html');
});
app.use('/js', express.static(__dirname + '/js'));
//app.listen(8080);
server.listen(8080);
console.log('daemon start on http://localhost:8080');