This repository has been archived by the owner on Jan 4, 2019. It is now read-only.
forked from byrichardpowell/draw
-
Notifications
You must be signed in to change notification settings - Fork 158
/
server.js
executable file
·217 lines (181 loc) · 6.16 KB
/
server.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
* Module dependencies.
*/
var settings = require('./src/util/Settings.js'),
tests = require('./src/util/tests.js'),
draw = require('./src/util/draw.js'),
projects = require('./src/util/projects.js'),
db = require('./src/util/db.js'),
express = require("express"),
paper = require('paper'),
socket = require('socket.io'),
async = require('async'),
fs = require('fs'),
cookieParser = require('cookie-parser');
http = require('http'),
https = require('https'),
session = require('express-session');
/**
* SSL Logic and Server bindings
*/
if(settings.ssl){
console.log("SSL Enabled");
console.log("SSL Key File" + settings.ssl.key);
console.log("SSL Cert Auth File" + settings.ssl.cert);
var options = {
key: fs.readFileSync(settings.ssl.key),
cert: fs.readFileSync(settings.ssl.cert)
};
var app = express(options);
var server = https.createServer(options, app).listen(settings.port);
}else{
var app = express();
var server = app.listen(settings.port);
}
/**
* Build Client Settings that we will send to the client
*/
var clientSettings = {
"tool": settings.tool
}
// Config Express to server static files from /
app.use(express.static(__dirname + '/'));
// Sessions
app.use(cookieParser());
app.use(session({secret: 'secret', key: 'express.sid'}));
// ROUTES
// Index page
app.get('/', function(req, res){
res.sendfile(__dirname + '/src/static/html/index.html');
});
// Drawings
app.get('/d/*', function(req, res){
res.sendfile(__dirname + '/src/static/html/draw.html');
});
// Front-end tests
app.get('/tests/frontend/specs_list.js', function(req, res){
tests.specsList(function(tests){
res.send("var specs_list = " + JSON.stringify(tests) + ";\n");
});
});
// Used for front-end tests
app.get('/tests/frontend', function (req, res) {
res.redirect('/tests/frontend/');
});
// Static files IE Javascript and CSS
app.use("/static", express.static(__dirname + '/src/static'));
// LISTEN FOR REQUESTS
var io = socket.listen(server);
io.sockets.setMaxListeners(0);
console.log("Access Etherdraw at http://"+settings.ip+":"+settings.port);
// SOCKET IO
io.sockets.on('connection', function (socket) {
socket.on('disconnect', function () {
console.log("Socket disconnected");
// TODO: We should have logic here to remove a drawing from memory as we did previously
});
// EVENT: User stops drawing something
// Having room as a parameter is not good for secure rooms
socket.on('draw:progress', function (room, uid, co_ordinates) {
if (!projects.projects[room] || !projects.projects[room].project) {
loadError(socket);
return;
}
io.in(room).emit('draw:progress', uid, co_ordinates);
draw.progressExternalPath(room, JSON.parse(co_ordinates), uid);
});
// EVENT: User stops drawing something
// Having room as a parameter is not good for secure rooms
socket.on('draw:end', function (room, uid, co_ordinates) {
if (!projects.projects[room] || !projects.projects[room].project) {
loadError(socket);
return;
}
io.in(room).emit('draw:end', uid, co_ordinates);
draw.endExternalPath(room, JSON.parse(co_ordinates), uid);
});
// User joins a room
socket.on('subscribe', function(data) {
subscribe(socket, data);
});
// User clears canvas
socket.on('canvas:clear', function(room) {
if (!projects.projects[room] || !projects.projects[room].project) {
loadError(socket);
return;
}
draw.clearCanvas(room);
io.in(room).emit('canvas:clear');
});
// User removes an item
socket.on('item:remove', function(room, uid, itemName) {
draw.removeItem(room, uid, itemName);
io.sockets.in(room).emit('item:remove', uid, itemName);
});
// User moves one or more items on their canvas - progress
socket.on('item:move:progress', function(room, uid, itemNames, delta) {
draw.moveItemsProgress(room, uid, itemNames, delta);
if (itemNames) {
io.sockets.in(room).emit('item:move', uid, itemNames, delta);
}
});
// User moves one or more items on their canvas - end
socket.on('item:move:end', function(room, uid, itemNames, delta) {
draw.moveItemsEnd(room, uid, itemNames, delta);
if (itemNames) {
io.sockets.in(room).emit('item:move', uid, itemNames, delta);
}
});
// User adds a raster image
socket.on('image:add', function(room, uid, data, position, name) {
draw.addImage(room, uid, data, position, name);
io.sockets.in(room).emit('image:add', uid, data, position, name);
});
});
// Subscribe a client to a room
function subscribe(socket, data) {
var room = data.room;
// Subscribe the client to the room
socket.join(room);
// If the close timer is set, cancel it
// if (closeTimer[room]) {
// clearTimeout(closeTimer[room]);
// }
// Send settings
socket.emit('settings', clientSettings);
// Create Paperjs instance for this room if it doesn't exist
var project = projects.projects[room];
if (!project) {
console.log("made room");
projects.projects[room] = {};
// Use the view from the default project. This project is the default
// one created when paper is instantiated. Nothing is ever written to
// this project as each room has its own project. We share the View
// object but that just helps it "draw" stuff to the invisible server
// canvas.
projects.projects[room].project = new paper.Project();
projects.projects[room].external_paths = {};
db.load(room, socket);
} else { // Project exists in memory, no need to load from database
loadFromMemory(room, socket);
}
// Broadcast to room the new user count -- currently broken
var rooms = socket.adapter.rooms[room];
var roomUserCount = Object.keys(rooms).length;
io.to(room).emit('user:connect', roomUserCount);
}
// Send current project to new client
function loadFromMemory(room, socket) {
var project = projects.projects[room].project;
if (!project) { // Additional backup check, just in case
db.load(room, socket);
return;
}
socket.emit('loading:start');
var value = project.exportJSON();
socket.emit('project:load', {project: value});
socket.emit('loading:end');
}
function loadError(socket) {
socket.emit('project:load:error');
}