-
Notifications
You must be signed in to change notification settings - Fork 3
/
app.js
executable file
·181 lines (167 loc) · 6.51 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/**
* Module dependencies.
*/
var express = require('express');
var routes = require('./routes');
var user = require('./routes/user');
var http = require('http');
var path = require('path');
var colors = require('colors');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
app.get('/', routes.index);
app.get('/users', user.list);
// console.log("Users" + user.list);
// I think there is a better way to do this
// app.get('/homepagetest', routes.homepagetest);
app.get("/aboutus", function(req, res) {
res.render("aboutus.jade");
});
var server = http.createServer(app).listen(app.get('port'));
var io = require('socket.io').listen(server, function() {
// console.log("Express server listening on port " + app.get('port'));
});
var canvasActivity = {};
var canvasActive = 0;
for (var k in canvasActivity) {
if (canvasActivity[k] == 1 || canvasActive == 1) {
canvasActive = 1;
}
}
var project;
// A user connects to the server (opens a socket)
io.sockets.on('connection', function(socket) {
socket.on('connectLogin', function() {
var clients = io.sockets.clients(socket.room);
// console.log("Client 1 " + clients[0]);
// console.log("Connect Login!".blue);
if (socket.room != null && socket.username != null && clients.length > 1) {
// console.log("passed check 1");
// console.log("Clients".red + clients[0].id + ", " + clients[clients.length - 1].id);
io.sockets.socket(clients[0].id).emit('getProject', socket.id);
}
});
socket.on('updateProject', function(data) {
io.sockets.socket(data.session).emit('setProject', data);
// console.log("Project layer!".red + data.project);
// console.log("Set Project");
});
socket.on('disconnect', function() {
var data = {
'message': socket.username + " disconnected",
pseudo: "<b>Server</b>"
}
if (socket.room != null) {
socket.broadcast.to(socket.room).emit('message', data);
socket.broadcast.to(socket.room).emit('disconnectedUser', socket.id);
}
});
/*********Login Functions**********/
socket.on('setPseudo', function(data) {
var oldname = data;
socket.set('pseudo', data);
socket.username = data;
// console.log('Pseudo: ', data);
var message = {
'message': "Your username is " + data,
pseudo: "<b>Server</b>"
};
socket.emit('message', message);
});
socket.on('setRoom', function(room) {
socket.room = room;
socket.join(room);
var data = {
'message': "Joined room " + room,
pseudo: "<b>Server</b>"
};
socket.emit('message', data);
var data = {
'message': socket.username + " joined " + socket.room,
pseudo: "<b>Server</b>"
};
socket.broadcast.to(socket.room).emit('message', data);
});
socket.on('changePseudo', function(newName, oldName) {
var oldname = newName;
socket.set('pseudo', newName);
socket.username = newName;
var message = {
'message': "Your username is now " + newName + ".",
pseudo: "<b>Server</b>"
};
socket.emit('message', message);
message = {
'message': oldName + " changed name to " + newName + ".",
pseudo: "<b>Server</b>"
};
socket.broadcast.to(socket.room).emit('message', message);
});
/*********Chat Functions**********/
socket.on('message', function(message) {
if (message == "/help") {
var helpMessage = "After selecting:<br>User arrow keys or click and drag to move<br>Press 'c' to change line color<br>Press 'f' to toggle fill<br>Press 't' to increase thickness<br>Press 'Shift+T' to decrease thickness<br>Press 'd' to duplicate<br>Press 'Shift+D' or 'Backspace' or 'delete' to delete.<br>Press 'Shift+<' to bring to front<br>Press 'Shift+>' to send to back<br>Press ',' to move forward<br>Press '.' to move backward";
var data = {
'message': helpMessage,
pseudo: "<b>Server</b>"
}
socket.emit('message', data);
} else {
var data = {
'message': message,
pseudo: "<b>" + socket.username + "</b>"
};
socket.broadcast.to(socket.room).emit('message', data);
}
});
/******Draw Functions********/
socket.on('endPath', function(data, session) {
console.log("session " + session + " completed path:");
socket.broadcast.to(socket.room).emit('endPath', data);
//canvasActivity[socket.user] = 0;
})
socket.on('addPoint', function(data, session) {
console.log("session " + session + " added:");
console.log(data);
socket.broadcast.to(socket.room).emit('addPoint', data);
// canvasActivity[socket.user] = 1;
})
socket.on('drawPath', function(data, session) {
console.log("session " + session + " drew:");
console.log(data);
socket.broadcast.to(socket.room).emit('drawPath', data);
// canvasActivity[socket.user] = 1;
})
socket.on('typeText', function(data, session) {
console.log("session " + session + " drew:");
console.log(data);
socket.broadcast.to(socket.room).emit('typeText', data);
// canvasActivity[socket.user] = 1;
})
socket.on('removePath', function(data, session) {
socket.broadcast.to(socket.room).emit('removePath', data);
})
socket.on('selectPath', function(data, session) {
socket.broadcast.to(socket.room).emit('selectPath', data);
})
socket.on('deselectPath', function(data, session) {
socket.broadcast.to(socket.room).emit('deselectPath', data);
})
socket.on('changeLayer', function(data, session) {
socket.broadcast.to(socket.room).emit('changeLayer', data);
})
});