-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (78 loc) · 2.77 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
89
90
91
92
93
94
95
96
97
98
99
var express = require('express'),
server = require('http'),
socket = require('socket.io'),
redis = require('redis');
var app = express(),
http = require('http'),
server = http.createServer(app).listen(8080),
io = require('socket.io').listen(server);
io.set('log level', 1);
//express configuration
app.use("/styles", express.static(__dirname + '/public/styles'));
app.use("/scripts", express.static(__dirname + '/public/scripts'));
app.use("/images", express.static(__dirname + '/public/images'));
//routes
app.get('/', function (req, res) {
res.sendfile(__dirname + '/public/index.html');
});
var redisClient = redis.createClient(process.env.REDIS_PORT, process.env.REDIS_HOST);
redisClient.auth(process.env.REDIS_SECRET, function (err) {
if (err) { throw err; }
});
//events
io.sockets.on('connection', function(client) {
client.once('join', function(user) {
join(client,user);
});
client.on('private', function(message) {
private_message(client, message);
});
client.on("disconnect", function(){
disconnect(client);
});
});
//helpers
function join(client,user){
client.set("user_name",user.name);
client.set("user_id",user.id);
client.join(user.name);
console.log("New connection: "+user.name);
notify_mutuals(client,user.id,
function(client,mutual){
client.emit("add_user", mutual);
console.log("NOTIFICO a: "+mutual.name + " que "+user.name + " se conecto.");
client.broadcast.to(mutual.name).emit('add_user',user);
});
storeUser(user);
}
function private_message(client,message){
client.broadcast.to(message.recipient).emit('private',message);
}
function disconnect(client){
client.get("user_id", function(err,id){
console.log('Client '+id+' disconnected');
redisClient.hset(redis_key(id,"object"), "status", "disconnected");
notify_mutuals(client,id,function(client,mutual){
console.log("NOTIFICO a: "+mutual.name + " que "+ id + "se desconecto");
client.broadcast.to(mutual.name).emit('user_disconnected',id);
});
});
}
function notify_mutuals(client,user_id,operation){
redisClient.sinter(redis_key(user_id,"i_said_are_hot"), redis_key(user_id,"said_im_hot"),
function(err,mutuals){
mutuals.forEach(function(m){
redisClient.hgetall(redis_key(m,"object"), function(err,mutual){
operation(client,mutual);
});
});
});
}
function storeUser(user){
redisClient.hmset(redis_key(user.id,"object"),"id",user.id,
"name",user.name,"image",user.image,"status", "connected");
}
function redis_key(id,str) {
return "user:"+id+":"+str;
}
console.log('Chat server is running...');