forked from rickyrauch/Balloons.IO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathballoons.js
136 lines (114 loc) · 3.42 KB
/
balloons.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
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
/*
* Module dependencies
*/
var express = require('express')
, sio = require('socket.io')
, easyoauth = require('easy-oauth')
, redis = require('redis')
, RedisStore = require('connect-redis')(express)
, config = require('./config.json')
, utils = require('./utils');
/*
* Instanciate redis
*/
var client = redis.createClient();
/*
* Create and config server
*/
var app = express.createServer();
app.configure(function(){
app.set('view engine', 'jade');
app.set('views', __dirname + '/views/themes/' + config.theme.name);
app.use(express.static(__dirname + '/public'));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({ secret: config.session.secret, store: new RedisStore }));
app.use(easyoauth(config.auth));
app.use(app.router);
});
/*
* Routes
*/
app.get('/', function(req,res,next){
req.authenticate(['oauth'], function(error, authenticated) {
if(authenticated) {
res.redirect('/rooms/list');
} else {
res.render('index');
}
});
});
app.get('/rooms/list', utils.restrict, function(req, res){
client.hgetall('rooms', function(err, rooms){
res.locals({'rooms' : rooms});
res.render('room_list');
});
});
app.post('/create', utils.restrict, function(req, res){
if(req.body.room_name.length <= 30) {
client.hget('rooms', req.body.room_name, function(err, room){
if(room){
res.redirect('/rooms/' + room);
} else {
client.hset('rooms', req.body.room_name, encodeURIComponent(req.body.room_name), function(err, id){
res.redirect('/rooms/' + encodeURIComponent(req.body.room_name));
});
}
});
} else {
res.redirect('back');
}
});
app.get('/rooms/:id', utils.restrict, function(req,res){
client.hgetall('rooms', function(err, rooms){
if(rooms[decodeURIComponent(req.params.id)]){
client.smembers('users:'+req.params.id, function(error, user_list){
res.locals({'rooms': rooms,'room_name':decodeURIComponent(req.params.id) ,'room_id':req.params.id,'username': req.getAuthDetails().user.username,'user_list':user_list});
res.render('room');
});
} else {
res.redirect('back');
}
});
});
/*
* Socket.io
*/
var io = sio.listen(app);
io.configure(function(){
io.set('store',new sio.RedisStore);
io.enable('browser client minification');
io.enable('browser client gzip');
});
io.sockets.on('connection', function (socket) {
socket.on('set nickname',function(data){
socket.join(data.room_id);
socket.set('nickname', data.nickname, function () {
socket.set('room_id', data.room_id, function () {
client.sadd('users:'+data.room_id, data.nickname, function(err, added){
if(added)
io.sockets.in(data.room_id).emit('new user',{'nickname':data.nickname});
});
});
});
});
socket.on('my msg',function(data){
socket.get('nickname',function(err,nickname){
socket.get('room_id',function(err,room_id){
var no_empty = data.msg.replace("\n","");
if(no_empty.length > 0)
io.sockets.in(room_id).emit('new msg',{'nickname':nickname,'msg':data.msg});
});
});
});
socket.on('disconnect',function(){
socket.get('nickname',function(err,nickname){
socket.get('room_id',function(e,room_id){
client.srem('users:'+room_id, nickname);
io.sockets.in(room_id).emit('user leave',{'nickname': nickname});
});
});
});
});
app.listen(3000);
console.log('Balloons.io started at port %d', app.address().port);