-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatlogic.js
69 lines (56 loc) · 1.53 KB
/
chatlogic.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
function chatlogic( socket ) {
var people = [];
// Create a new Person object for the given socket
people.push( new Person(socket) );
/*
* Joinlistener, adds additional data to the current person and tells everyone that he/she joined
*/
socket.on('join', function( data ) {
var person = people[findIndexBySocket( socket )];
person.name = data.name;
notifyEveryone( {message: person.name + ' has joined', name: 'System'} );
});
/*
* Messagelistener, distributes the given message to all connected people
*/
socket.on('message', function( messagedata ) {
var person = people[findIndexBySocket( socket )];
notifyEveryone( {message: messagedata.message, name: person.name} );
});
/*
* Disconnect logic
*/
socket.on('disconnect',function() {
// To prevent incorrect data in our people array, we need to remove the current person from the array
var index = findIndexBySocket( socket );
people.splice(index, 1);
});
/*
* Finds the index of the person with the given sock
et
*/
function findIndexBySocket( socket ) {
for( var i = 0; i < people.length; i++ ) {
if( people[i].socket == socket ) {
return i;
}
}
return null;
}
/*
* Sends the given message to all connected people
*/
function notifyEveryone( message ) {
for( var i = 0; i < people.length; i++ ) {
people[i].socket.emit('message', message);
}
}
}
/*
* The object containing all the data required for chatting
*/
function Person( socket ) {
this.socket = socket;
this.name = 'Anonymous';
}
module.exports = chatlogic;