-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (104 loc) · 3.3 KB
/
index.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
var express = require('express');
var socket = require('socket.io');
var favicon = require('serve-favicon');
// App setup
var app = express();
app.use(favicon('favicon.png'));
var server = app.listen(4000, function(){
console.log('listening to requests on port 4000');
});
// Static files
app.use(express.static('public'));
// Socket setup
var io = socket(server);
var statsSocket = 0;
var roomsArray = [];
io.on('connection', function(socket){
// to update stats
socket.on('stats', function(){
statsSocket = socket.id;
socket.emit('updateUsers', io.sockets.adapter.sids.size);
})
if (statsSocket){
io.to(statsSocket).emit('updateUsers', io.sockets.adapter.sids.size);
}
console.log('made socket connection', socket.id);
// Handle pairing event
var destination;
socket.on('search', function(){
if (io.sockets.adapter.rooms.get(destination) &&
io.sockets.adapter.rooms.get(destination).has(socket.id)){
quit_room(destination, socket.id);
return
}
roomFound = false;
for (room in roomsArray) {
if (io.sockets.adapter.rooms.get(roomsArray[room]).size < 2) {
destination = roomsArray[room];
socket.join(destination);
setTimeout(function(){
io.to(destination).emit('roomFound');
}, Math.floor(Math.random() * 4000));
// connect after random timeout
// to avoid instantly connecting to the same person
roomFound = true;
break;
}
}
if (!roomFound) {
var taken = true;
while (taken){
destination = Math.random().toString(36).substr(2, 5);
taken = ((roomsArray.includes(destination)) ? true : false);
}
roomsArray.push(destination);
socket.join(destination);
}
});
// Handle chat event
socket.on('chat', function(data){
if (io.sockets.adapter.rooms.get(destination) &&
io.sockets.adapter.rooms.get(destination).has(socket.id) &&
typeof data.message !== 'undefined'){
data = {
message: data.message.replace(/</g, "<").replace(/>/g, ">")
}
if (data.message.replace(/\s/g, '').length){
io.to(destination).emit('chat', data, socket.id);
}
}
});
var typeFeedback;
socket.on('typing', function(){
if (io.sockets.adapter.rooms.get(destination) &&
io.sockets.adapter.rooms.get(destination).has(socket.id)){
io.to(destination).emit('typing', socket.id);
clearTimeout(typeFeedback);
}
});
socket.on('stopTyping', function() {
typeFeedback = setTimeout(function() {
io.to(destination).emit('removeFeedback', socket.id);
}, 5000);
});
// Handle Stop event
socket.on('stop', function(){
quit_room(destination, socket.id);
});
// Disconnect logic
socket.on('disconnect', function(){
io.to(statsSocket).emit('updateUsers', io.sockets.adapter.sids.size);
console.log('disconnect');
quit_room(destination, socket.id);
});
});
function quit_room(destination, socketId){
const index = roomsArray.indexOf(destination);
if (index > -1){
roomsArray.splice(index, 1);
}
io.to(destination).emit('strangerQuit', socketId);
io.sockets.adapter.rooms.delete(destination);
destination = "";
}
//