/
server.js
executable file
·105 lines (89 loc) · 2.83 KB
/
server.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
var app = require('http').createServer(handler);
var redis = require('redis');
var sio = require('socket.io'),
RedisStore = sio.RedisStore,
io = sio.listen(app);
var fs = require('fs');
var config = require('./config');
// http config
app.listen(config.port);
console.log('Server started, listening on port ' + config.port);
function handler(req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
// redis client config
var port = config.redis.port,
host = config.redis.host,
pass = config.redis.password;
// redis client init
var pub = redis.createClient(port, host);
var sub = redis.createClient(port, host);
var store = redis.createClient(port, host);
// redis auth
pub.auth(pass, function(err) {});
sub.auth(pass, function(err) {});
store.auth(pass, function(err) {});
// socket.io config
io.configure(function() {
io.set('store', new RedisStore({ redisPub: pub, redisSub: sub, redisClient: store }));
});
// socket.io client event listeners
io.sockets.on('connection', function (socket) {
var rc = redis.createClient(port, host);
rc.auth(pass, function(err) {});
var publishUsers = function() {
// publish updated user list
rc.smembers('users', function(err, res) {
// update the list of users in chat, client-side
io.sockets.emit('user:update', res);
});
};
var publishChat = function(message) {
rc.hgetall(message, function(err, res) {
console.log(res);
rc.hget('user:' + res.uid, 'name', function(err, username) {
io.sockets.emit('chat:update', username, res.text);
});
});
};
socket.on('user:add', function(username) {
// add user to redis set
rc.incr('users:uid:next', function(err, uid) {
rc.multi()
.hmset('user:' + uid, { name: username })
.set('uid:' + username, uid)
.sadd('users', username)
.exec(function(err, res) {
// store the username and uid in the socket session for this client
socket.username = username;
socket.uid = uid;
// echo globally (all clients) that a person has connected
io.sockets.emit('chat:update', 'SERVER', socket.username + ' has connected');
publishUsers();
});
});
})
.on('chat:send', function (data) {
rc.incr('messages:id:next', function(err, id) {
rc.hmset('message:' + id, { uid: socket.uid, text: data }, function(err, res) {
publishChat('message:' + id);
});
});
})
.on('disconnect', function() {
socket.broadcast.emit('chat:update', 'SERVER', socket.username + ' has disconnected');
// remove user from redis set
rc.srem('users', socket.username, function(err, res) {
publishUsers();
rc.quit();
});
});
});