-
Notifications
You must be signed in to change notification settings - Fork 8
/
app.js
62 lines (44 loc) · 1.36 KB
/
app.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
var express = require('express');
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(3000);
app.use(express.static('public'))
var ipList = [];
io.on('connection', function (socket) {
var newIp = checkIfIpAlreadyExist(socket.handshake.address)
if (newIp) {
//Send to all listners except this
socket.broadcast.emit('new_ip', {new_ip: newIp});
//send message only to sender-client
socket.emit('ip_list', {list : ipList});
//io.emit('new_ip', {new_ip: newIp});
}
//if it is new all
//then
//this new ip should get full ipList
//and
//all others except this ip shoule get single new ip add
socket.on('disconnect', function () {
removeDisconnectedIps(socket.handshake.address)
io.emit('remove_disconnected_ip', {removed: socket.handshake.address});
});
});
function checkIfIpAlreadyExist(ip) {
function isItThere(value) {
return value == ip;
}
var filtered = ipList.filter(isItThere);
if (filtered.length == 0) {
ipList.push(ip)
return ip;
}
}
function removeDisconnectedIps(ip) {
ipList.forEach(function (listip, index) {
if (listip == ip) {
ipList.splice(index, 1);
}
})
console.log('Final list ' + JSON.stringify(ipList))
}