-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
121 lines (95 loc) · 3.04 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Module dependencies.
*/
var express = require('express')
, connect = require('connect')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
var server = http.createServer(app);
var io = require('socket.io').listen(server);
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
app.configure('development', function(){
app.use(express.errorHandler());
});
//Get request to the index page
app.get('/', function(req,res){
res.render('index');
});
app.get('/karthik',function(req,res){
res.redirect('www.github.com/karthikkp');
})
app.get('/admin',function(req,res){
res.render('admin',{rooms:io.sockets.manager.rooms});
})
//Socket functions
io.sockets.on('connection',function(socket){
//when ever there is a new socket connection
//we update the rooms on that user
socket.emit('update_rooms',io.sockets.manager.rooms);
socket.on('new_user',function(nickname){
socket.name = nickname;
});
//On creating a new room,we check
//1.if user is already in a room
//2.if he is, then we check if that
//room will be empty if the user leaves
//3.if it is then we emit to all sockets to remove that room the page
//then the users leaves the room,joins the new room
//and socket.room is updated with the new room
socket.on('new_room',function(room){
if(socket.room){
if(io.sockets.manager.rooms["/"+socket.room].length<2){
io.sockets.emit('remove_room',socket.room);
}
socket.leave(socket.room);
}
socket.room=room;
socket.join(room);
socket.broadcast.emit('new_room',room);
});
//changing a room ,something similar to what is done on a new room is done
socket.on('change_room',function(room){
if(io.sockets.manager.rooms[room].length<2){
if(socket.room){
if(io.sockets.manager.rooms["/"+socket.room].length<2){
io.sockets.emit('remove_room',socket.room);
}
socket.leave(socket.room);
}
socket.join(room);
socket.room = room;
// we emit a status message to that socket telling if the user was able to join or not
socket.emit('status_of_join',true);
}
else{
socket.emit('status_of_join',false);
}
});
//socket events to draw on the canvas
socket.on('mousedown',function(x,y){
io.sockets.in(socket.room).emit('mousedownn',x,y);
});
socket.on('mouseup',function(){
io.sockets.in(socket.room).emit('mouseupp');
});
socket.on('mousemove',function(x,y){
io.sockets.in(socket.room).emit('mousemovee',x,y);
});
});
server.listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});