This repository has been archived by the owner on Aug 30, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socketIo.js
234 lines (195 loc) · 6.33 KB
/
socketIo.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
var app = require('../../app');
var config = require('config');
var Chatroom = require('../models/chatroom').Chatroom;
var Chat = require('../models/chat').Chat;
var CONST = require('../const').CONST;
var utils = require('../utils');
var check = require('validator').check;
var cookieLib = require('cookie'); // cookie 操作npmモジュール
var redis = require('socket.io/node_modules/redis');
var redisClient = redis.createClient(config.redis.port, config.redis.host);
var ownerClients = {}; //ハウスオーナーのSocket.ioのセッションを保持
exports.onConnection = function(socket) {
var logger = app.set('logger');
var sessionStore = app.set('sessionStore');
var chatroomId = socket.handshake.query.id;
var isUrlOpen = socket.handshake.query.urlopen;
var user;
var _csrf;
logger.info('##socket.io: connection');
//validation
try {
check(chatroomId).isInt();
} catch (e) {
logger.error(e.message);
return;
}
//部屋に接続してきたユーザーのセッションIDを部屋ごとに保持
//## Socket.ioのnamespaceの独自実装
redisClient.hset("chatroomClients:"+chatroomId, socket.id, "true");
// // ExpressとSocket.io間でセッションを共有
if (socket.handshake.headers.cookie) {
var cookie = socket.handshake.headers.cookie;
var sessionId = cookieLib.parse(cookie)['sess_id'];
sessionId = sessionId.split(':')[1].split('.')[0];
sessionStore.get(sessionId, function(err, session) {
if (err) { logger.error(err); return; }
if (session) {
user = session.passport.user;
_csrf = session._csrf;
}
});
}
// 招待機能を使わないため一時コメントアウト
// Chatroom.findOneById(chatroomId, function(err, chatroom) {
// if (err) { logger.error(err); return; }
// //socket.ioにおけるオーナーのセッションIDを識別
// // if (user && (user.id === chatroom.ownerId)) {
// // var key = isUrlOpen ? chatroomId + '/open' : chatroomId;
// // ownerClients[key] = socket;
// // }
// });
socket.on('message', function(data) {
logger.info('##socket.io: message');
//未ログイン時は以降の処理を行わない
if (!user) { return; }
//CSRF Check
if (_csrf && _csrf !== data.token) {
logger.error('CSRF Invalid');
return;
}
//validation
try {
check(data.chatroomId).isInt();
check(data.userId).isInt();
if (data.message) {
check(data.message).len(0, 10000);
}
if (data.type) {
check(data.type).isInt();
}
} catch (e) {
logger.error(e.message);
return;
}
Chatroom.findOneById(data.chatroomId, function(err, chatroom) {
if (err) { logger.error(err); return; }
data.userId = user.id;
data.ownerId = chatroom.ownerId;
if (data.mode === 'create') {
if (chatroom.status !== CONST.STATUS_OPEN &&
!data.isUrlOpen) {
createEntry(socket, data);
}
else {
createChat(socket, data);
}
}
else if (data.mode === 'destroy') {
destroy(socket, data);
}
});
});
socket.on('disconnect', function() {
logger.info('##socket.io: disconnect');
});
};
function createEntry(socket, data) {
var io = app.set('io');
Chat.create({
chatroomId: data.chatroomId,
userId: data.userId,
message: data.message,
type: data.type,
status: CONST.STATUS_ENTRY
}, function(err, results) {
if (err) { logger.error(err); return; }
data.chatId = results[0].id;
data.time = results[0].time;
data.message = utils.nl2br(utils.escHtml(data.message));
var user = socket.handshake.session.passport.user;
var isUrlOpen = socket.handshake.query.urlopen;
//オーナーの発言はブロードキャスト
if (user && (user.id === data.ownerId)) {
//socket.emit('message', data);
//socket.broadcast.emit('message', data);
redisClient.hkeys("chatroomClients:"+data.chatroomId, function(err, clients) {
clients.forEach(function(sid) {
process.nextTick(function() {
io.sockets.socket(sid).emit('message', data);
});
});
});
}
else {
//参加申請での申込者の発言はオーナーと申込者本人のみにemit
var key = isUrlOpen ? data.chatroomId + '/open' : data.chatroomId;
var ownerSocket = ownerClients[key];
if (ownerSocket) {
ownerSocket.emit('message', data);
}
socket.emit('message', data);
}
});
}
function createChat(socket, data) {
var io = app.set('io');
Chat.create({
chatroomId: data.chatroomId,
userId: data.userId,
message: data.message,
type: data.type,
status: CONST.STATUS_CHAT
}, function(err, results) {
if (err) { logger.error(err); return; }
data.chatId = results[0].id;
data.time = results[0].time;
data.message = utils.nl2br(utils.escHtml(data.message));
//socket.emit('message', data);
//socket.broadcast.emit('message', data);
redisClient.hkeys("chatroomClients:"+data.chatroomId, function(err, clients) {
clients.forEach(function(sid) {
process.nextTick(function() {
io.sockets.socket(sid).emit('message', data);
});
});
});
});
}
function destroy(socket, data) {
var io = app.set('io');
var chatId = data.chatId;
//validation
try {
check(chatId).isInt();
} catch (e) {
logger.error(e.message);
return;
}
Chat.findOneById(chatId, function(err, chat) {
if (err) { logger.error(err); return; }
if (chat.userId === data.userId) {
Chat.destroy({
chatId: chatId
}, function(err2, results) {
if (err2) { logger.error(err2); return; }
data.chatId = chatId;
//socket.emit('message', data);
//socket.broadcast.emit('message', data);
redisClient.hkeys("chatroomClients:"+data.chatroomId, function(err, clients) {
clients.forEach(function(sid) {
process.nextTick(function() {
io.sockets.socket(sid).emit('message', data);
});
});
});
});
}
});
}
exports.onDisconnect = function(socket) {
var logger = app.set('logger');
var chatroomId = socket.handshake.query.id;
logger.info('##socket.io: disconnect');
redisClient.hdel("chatroomClients:"+chatroomId, socket.id);
};