-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
160 lines (134 loc) · 4.65 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
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var paper = require('paper');
var passport = require('passport');
var session = require('express-session');
var db = require("./db/operations.js")
var app = express();
//creating server and connecting socket with server
var server = require('http').createServer(app);
var io = require('socket.io')(server);
require('./config/passport'); // pass passport for configuration
//===============EXPRESS================
// Configure Engines
app.set('views', path.join(__dirname, 'views'));
//===============EXPRESS================
// Configure Express
app.use(logger('dev'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.set('view engine', 'ejs');
// required for passport
app.use(session({
secret: 'ilovescotchscotchyscotchscotch', // session secret
resave: true,
saveUninitialized: true
}));
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session
require('./routes/routes.js')(app, passport); // load our routes and pass in our app and fully configured passport
/// catch 404 and forwarding to error handler
app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
/// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: err
});
});
}
// production error handler
// no stacktraces leaked to user
app.use(function (err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
});
});
// server listen
server.listen(3000);
var double_p = require('./routes/double_p.js');
var draw = require('./routes/draw.js');
io.sockets.on('connection', function (socket) {
socket.on('disconnect', function () {
console.log("disconnecting");
});
socket.on('path_request', function (room, id, point, color) {
io.in(room).emit('path_request_u', id, point, color);
draw.draw(room, point, id, color);
});
socket.on('path_point', function (room, id, point) {
io.in(room).emit('path_point_u', id, point);
draw.add_point(room, point, id);
});
socket.on('path_end', function (room, id) {
io.in(room).emit('path_end_u', id);
draw.draw_end(room, id);
});
socket.on('rect', function (room, id, pos, size, color) {
io.in(room).emit('rect_u', pos, size, color);
draw.draw_rect(id,room, pos, size, color);
});
socket.on('circle', function (room, id, pos, size, color) {
io.in(room).emit('circle_u', pos, size, color);
draw.draw_circle(id,room, pos, size, color);
});
socket.on('msg', function (room, id, msg) {
io.in(room).emit('msg_u', id, msg);
});
socket.on('join', function (room, id) {
joinn(socket, room, id);
});
});
function joinn(socket, room, id) {
console.log("joining - - - - ");
socket.join(room);
paths = double_p.paths;
projects = double_p.projects;
var project = double_p.projects[room];
if (!project) {
db.checkExistence(room, function (checker) {
if (checker) {
db.getJSON(room, function (project_json) {
paths[room] = {};
projects[room] = new paper.Project();
projects[room].importJSON(project_json);
io.in(room).emit('join:load_page', project_json);
});
}
else {
console.log("the project have been found in database");
paths = double_p.paths;
projects = double_p.projects;
paths[room] = {};
projects[room] = new paper.Project();
console.log(room);
db.roomCreateUpdate(id, room, projects[room].exportJSON());
}
});
}
else {
console.log("the project have been found in dict");
var project_json = projects[room].exportJSON();
io.in(room).emit('join:load_page', project_json);
}
}
module.exports = app;