forked from sekai013/squidboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io-chat-server.js
102 lines (94 loc) · 3.39 KB
/
io-chat-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
var app = module.parent.exports;
var io = app.get('io');
var url = require('url');
var Board = require('./mongodb-model');
var pathnameRegExp = /^\/boards\/([0-9a-z]+)$/;
var logger = require('./log-config').getLogger('post');
var getBoardId = function(socket) {
// if connection is valid, return id of target board
// else (e.g., connected from non-board page like /about) return null
var originUrl = socket.handshake.headers.referer;
var pathname = url.parse(originUrl).pathname;
var execResult = pathnameRegExp.exec(pathname);
return (execResult === null) ? null : execResult[1];
}
io.on('connection', function(socket) {
var boardId = getBoardId(socket);
try {
Board.findOne({ _id: boardId }, function(err, board) {
if (err) throw err;
if (!board) return;
socket.join(boardId);
socket.emit('cacheInitialize', board.canvas);
});
const PostsMaxLen = 500;
const NameMaxLen = 20;
const MessageMaxLen = 100;
var formatDate = function(date) {
var hours = date.getHours();
var minutes = date.getMinutes();
var hh = (hours > 9) ? hours : '0' + hours;
var mm = (minutes > 9) ? minutes : '0' + minutes;
return hh + ':' + mm;
};
socket.on('post', function(post) {
Board.findOne({ _id: boardId }, function(err, board) {
try {
if (err) throw err;
if (!board) throw new Error('Not Found');
if (post.name > NameMaxLen || post.message > MessageMaxLen) {
return;
}
var now = new Date();
post.time = formatDate(now);
board.posts.push(post);
if (board.posts.length > PostsMaxLen) {
board.posts = board.posts.slice(board.posts.length - PostsMaxLen);
}
board.lastModified = Date.now();
board.save(function(err) {
try {
if (err) throw err;
io.to(boardId).emit('newPost', post);
logger.info(socket.handshake.address + ': posted ' + post.message + ' to board ' + boardId + ' as ' + post.name);
} catch (e) {
logger.fatal(socket.handshake.address + ': attempted to post ' + post.message + ' to board ' + boardId + ' as ' + post.name + ' but failed');
logger.fatal(e.name + ':' + e.message);
}
});
} catch (e) {
logger.fatal(socket.handshake.address + ': attempted to post ' + post.message + ' to board ' + boardId + ' as ' + post.name + ' but failed');
logger.fatal(e.name + ':' + e.message);
}
});
});
const CacheMaxLen = 15
socket.on('cache', function(canvas) {
if (canvas.cache.length > CacheMaxLen) {
canvas.cache = canvas.cache.slice(canvas.cache.length - CacheMaxLen);
canvas.cacheLevel = CacheMaxLen - 1;
}
Board.findOne({ _id: boardId }, function(err, board) {
try {
if (err) throw err;
board.canvas = canvas;
board.lastModified = Date.now();
board.save(function(err) {
try {
if (err) throw err;
socket.broadcast.to(boardId).emit('cacheUpdate', canvas);
} catch (e) {
logger.fatal(socket.handshake.address + ': attempted to cache to board ' + boardId + ' but failed');
logger.fatal(e.name + ':' + e.message);
}
});
} catch (e) {
logger.fatal(socket.handshake.address + ': attempted to cache to board ' + boardId + ' but failed');
logger.fatal(e.name + ':' + e.message);
}
});
});
} catch (e) {
logger.warn('invalid socket connection: ip ' + socket.handshake.address + ' page ' + socket.handshake.headers.referrer);
}
});