-
Notifications
You must be signed in to change notification settings - Fork 1
/
socket.js
117 lines (103 loc) · 3.37 KB
/
socket.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
// This file is required by app.js.
// It handles all the server-side socketIO logic for CifraChat, interacting
// with the client-side code in /public/js/chat.js.
// discussion: https://github.com/Automattic/socket.io/issues/1450
var roomCount = function(room)
{
var localCount = 0;
if (room)
for (var id in room)
localCount ++;
return localCount;
}
// messages must be numbered to notify each client when their message is decrypted
var messageNum = 1;
var MAX_ALLOWED_CLIENTS = 2;
module.exports = function(app, io)
{
var chat = io.of('/chat').on('connection', function(clntSocket)
{
// each client is put into a chat room restricted to max 2 clients
clntSocket.on('joinRoom', function(room_id)
{
var clients_in_room = roomCount(chat.adapter.rooms[room_id]);
// client may only join room only if it's not full
if (clients_in_room >= MAX_ALLOWED_CLIENTS)
{
clntSocket.emit('serverMessage', {
message: 'This room is full.'
});
// force client to disconnect
clntSocket.disconnect();
}
else
{
// client joins room specified in URL
clntSocket.join(room_id);
clients_in_room++;
// welcome client on succesful connection
clntSocket.emit('serverMessage', {
message: 'Welcome to CifraChat. Send someone a link to this room to start chatting. Max 2 people per room.'
});
// let other user know that client joined
clntSocket.broadcast.to(room_id).emit('serverMessage', {
message: '<b>Other</b> has joined.'
});
if (clients_in_room == MAX_ALLOWED_CLIENTS){
// let everyone know that the max amount of users (2) has been reached
chat.in(room_id).emit('serverMessage', {
message: 'This room is now full. There are <b>2</b> users present.'
});
}
/** sending encrypted **/
clntSocket.on('cryptSend', function (data) {
// all data sent by client is sent to room
clntSocket.broadcast.to(room_id).emit('cryptMessage', {
message: data.message,
hint: data.hint,
sender: 'Other',
number: messageNum
});
// and then shown to client
clntSocket.emit('cryptMessage', {
message: data.message,
hint: data.hint,
sender: 'Self',
number: messageNum
});
messageNum++;
});
/** sending unencrypted **/
clntSocket.on('noncryptSend', function (text) {
// all data sent by client is sent to room
clntSocket.broadcast.to(room_id).emit('noncryptMessage', {
message: text.message,
sender: 'Other'
});
// and then shown to client
clntSocket.emit('noncryptMessage', {
message: text.message,
sender: 'Self'
});
// unencrypted messages don't increment messageNum because messageNum is only used to identify which message was decrypted
});
/** notifying clients of decryption **/
clntSocket.on('confirmDecrypt', function(id) {
// let room know which particular message was decrypted
chat.in(room_id).emit('markDecryption', id);
});
clntSocket.on('confirmMessageDestroy', function(id) {
chat.in(room_id).emit('markMessageDestroy', id)
});
};
/** disconnect listener **/
// notify others that somebody left the chat
clntSocket.on('disconnect', function() {
// let room know that this client has left
clntSocket.broadcast.to(room_id).emit('serverMessage', {
message: '<b>Other</b> has left.'
});
});
}); // end joinRoom listener
});
};