-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverapp.js
131 lines (120 loc) · 3.64 KB
/
serverapp.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
/**
* rewebrtc-server project
*
* Tho Q Luong <[email protected]>
* Feb 12, 2017
*/
var express = require('express');
var app = express();
// var path = require("path");
// var fs = require("fs");
// var open = require("open");
// var httpsOptions = {
// key: fs.readFileSync("./fake-keys/privatekey.pem"),
// cert: fs.readFileSync("./fake-keys/certificate.pem")
// };
// let isLocal = process.env.PORT == null;
// var serverPort = process.env.PORT || 8080;
// var server = null;
var server = require('http').createServer(app);
var io = require('socket.io')(server);
const users = {};
let socketIdToNames = {};
//------------------------------------------------------------------------------
// Serving static files
server.listen(process.env.PORT || 8080, function() {
console.log('server is running at 8080');
});
//------------------------------------------------------------------------------
// WebRTC Signaling
function socketIdsInRoom(roomId) {
var socketIds = io.nsps['/'].adapter.rooms[roomId];
if (socketIds) {
var collection = [];
for (var key in socketIds) {
collection.push(key);
}
return collection;
} else {
return [];
}
}
io.on('connection', function(socket) {
console.log('connected', socket.id);
socket.on('init', (data) => {
users[data.user] = socket;
});
socket.on('disconnect', function() {
console.log('in the disconnnect', socket.id);
delete users[socketIdToNames[socket.id]];
delete socketIdToNames[socket.id];
let room = socket.room;
if (socket.room) {
io.to(room).emit('disconnect', socket.id);
socket.leave(room);
}
});
socket.on('endCall', function({ fromUser, toUser }) {
console.log('end call,socket.id', socket.id);
console.log('from and tooooooooooooo', fromUser, toUser);
const roomId = [ fromUser, toUser ].sort().join('');
socket.leave(roomId);
users[toUser.toString()].leave(roomId);
users[toUser.toString()].emit('endCall', { fromUser });
});
/**
* Callback: list of {socketId, name: name of user}
*/
socket.on('calling', function({ fromUser, toUser }) {
console.log('to user0----', toUser);
users[toUser.toString()].emit('calling', { fromUser });
});
socket.on('reject', function({ fromUser, toUser }) {
console.log('to user0----', toUser);
const anotherUserSocket = users[toUser.toString()];
anotherUserSocket.emit('reject', { fromUser });
anotherUserSocket.leave([ fromUser.toString(), toUser.toString() ].sort().join(''));
// const room = anotherUserSocket.room;
// if (room) anotherUserSocket.leave(room);
});
socket.on('join', function(joinData, callback) {
//Join room
let roomId = joinData.roomId;
let name = joinData.name;
socket.join(roomId);
socket.room = roomId;
socketIdToNames[socket.id] = name;
var socketIds = socketIdsInRoom(roomId);
let friends = socketIds
.map((socketId) => {
return {
socketId: socketId,
name: socketIdToNames[socketId]
};
})
.filter((friend) => friend.socketId != socket.id);
callback(friends);
//broad
friends.forEach((friend) => {
io.sockets.connected[friend.socketId].emit('join', {
socketId: socket.id,
name
});
});
console.log('Join: ', joinData, io.sockets.connected);
});
socket.on('exchange', function(data) {
console.log('exchange', data);
console.log('socket.id--', socket.id);
data.from = socket.id.toString();
console.log('lsjfdlsfdkfjslfjsodf', data.to);
console.log('socket data', io.sockets.connected);
var to = io.sockets.connected[data.to.toString()];
console.log('to--->>', to);
to.emit('exchange', data);
});
socket.on('count', function(roomId, callback) {
var socketIds = socketIdsInRoom(roomId);
callback(socketIds.length);
});
});