-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.js
72 lines (58 loc) · 1.81 KB
/
web.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
var express = require('express');
var sio = require('socket.io');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Path = mongoose.model('Path', new Schema({
room: { type: String },
points: {},
updated_at: { type: Date, default: Date.now }
}));
var mongo_url = 'mongodb://localhost/paint';
if(process.env.MONGOHQ_URL){
mongo_url = process.env.MONGOHQ_URL;
}
var db = mongoose.connect(mongo_url);
var app = express.createServer(express.logger());
app.set('views', __dirname + '/views');
app.set('view options', {layout:false});
app.use(express.static(__dirname + '/public'));
app.get('/', function(req, res){
res.render('index.jade', {rooms: io.store.rooms});
});
app.get(/\/c\/([^\/]+)\/clear/, function(req, res){
var room = req.params[0];
Path.remove({room:room}, function(){});
io.sockets.in(room).emit('clear');
res.send('clear ' + room);
});
app.get(/\/c\/([^\/]+)/, function(req, res){
res.render('canvas.jade', {room: req.params[0]});
});
var port = process.env.PORT || 3000
app.listen(port, function(){
console.log("Listening on " + port);
});
var io = sio.listen(app);
io.sockets.on('connection', function(socket){
console.log("connect");
socket.on('join', function(room){
socket.join(room);
socket.room = room;
Path.find({room:room}, function(err, paths){
paths.forEach(function(path){
//console.log(path);
socket.emit('path', path.points);
});
});
});
socket.on('path', function(points){
var path = new Path();
path.room = socket.room;
path.points = points;
path.save();
socket.broadcast.to(socket.room).emit('path', points);
});
socket.on('disconnect', function(){
console.log("disconnect");
});
});