-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblotjotserver.js
130 lines (116 loc) · 3.6 KB
/
blotjotserver.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
var sys = require('sys');
var http = require('http');
var events = require('events');
var express = require('express');
var connect = require('connect');
var Artist = require('./artist').Artist;
var room = require('./room');
var app = express.createServer();
app.use(express.static(__dirname + '/public'));
function agentType(agent) {
var agent = agent.toLowerCase();
if (agent.indexOf("ipod") >= 0) return "ipod";
if (agent.indexOf("iphone") >= 0) return "iphone";
if (agent.indexOf("ipad") >= 0) return "ipad";
return "browser";
}
function renderIOS(req,res, board_type) {
res.render('board-ios.jade', {
layout: false,
locals: {
'room_id':req.params.room_id,
'board_type':board_type,
}
});
}
// HTTP Handlers
app.get('/', function( req, res) {
res.render('homepage.jade', {
layout:false,
locals: {
}
})
//res.redirect('/' + Math.floor(Math.random() * 10000));
});
app.get('/create', function( req, res) {
// Validate params
var type = req.param('type') ? req.param('type').toLowerCase() : null;
var room_id = req.param('room_id') ? req.param('room_id').toLowerCase() : null;
if (type != 'iphone' && type != 'ipad') {
throw Error("Type must be iphone or ipad");
}
if (!room_id || room_id.search(/^[a-zA-Z0-9]+$/) < 0) {
throw Error("Room ID must be only letters and numbers");
}
// Try to create the room
room.createRoom(room_id,type, function(success) {
if (success) {
res.redirect('/' + room_id);
} else {
throw Error("Room already exists");
}
});
});
app.get('/:room_id', function(req, res){
room.getRoom(req.params.room_id, function(room) {
if (room) {
var ua_type = agentType(req.headers['user-agent']);
console.log(ua_type);
if (ua_type == 'browser') {
res.render('board-browser.jade', {
layout: false,
locals: {
'room_id':req.params.room_id,
'board_type':room.type
}
});
} else {
renderIOS(req,res, room.type);
}
} else {
res.send("Room not found",404);
}
});
});
app.get('/:room_id/ios', function(req, res){
renderIOS(req,res);
});
app.get('/:room_id/test', function(req, res){
res.render('board-test.jade', {
layout: false,
locals: {
'room_id':req.params.room_id
}
});
});
app.listen(3000);
// Socket.IO API Handlers
var sio = require('socket.io');
var io = sio.listen(app, {
'transports':['websocket', 'server-events', 'htmlfile', 'xhr-multipart', 'xhr-polling']
});
io.sockets.on('connection', function(client){
// Converts messages into JSON messages
client.on('message', function (data) {
try {
var message = JSON.parse(data);
} catch (e) {
console.log("Couldn't parse JSON message: " + data + e);
return;
}
// Wait for init and only execute once
if (message.kind == 'init') {
room.getRoom(message['room_id'], function(room) {
if (room) {
client.emit("message",JSON.stringify({'kind':'initResponse','ready':true}));
new Artist(client, room);
} else {
throw Error("Room not found");
}
});
}
});
});
process.on('uncaughtException', function(e) {
console.log("UNCAUGHT EXCEPTION\n" + e.stack);
});