-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathapp.js
executable file
·116 lines (105 loc) · 3.03 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
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
/**
* 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 || 4443);
var server = null;
if (isLocal) {
server = require('https').createServer(httpsOptions, app);
} else {
server = require('http').createServer(app);
}
var io = require('socket.io')(server);
let socketIdToNames = {};
//------------------------------------------------------------------------------
// Serving static files
app.get('/', function(req, res){
console.log('get /');
res.sendFile(__dirname + '/index.html');
});
app.get('/draw', function(req, res){
console.log('get /');
res.sendFile(__dirname + '/draw.html');
});
app.use('/style', express.static(path.join(__dirname, 'style')));
app.use('/script', express.static(path.join(__dirname, 'script')));
app.use('/image', express.static(path.join(__dirname, 'image')));
server.listen(serverPort, function(){
console.log('Rewebrtc-server is up and running at %s port', serverPort);
if (isLocal) {
open('https://localhost:' + serverPort)
}
});
//------------------------------------------------------------------------------
// 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('Connection');
socket.on('disconnect', function(){
console.log('Disconnect');
delete socketIdToNames[socket.id];
if (socket.room) {
var room = socket.room;
io.to(room).emit('leave', socket.id);
socket.leave(room);
}
});
/**
* Callback: list of {socketId, name: name of user}
*/
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);
//broadcast
friends.forEach((friend) => {
io.sockets.connected[friend.socketId].emit("join", {
socketId: socket.id, name
});
});
console.log('Join: ', joinData);
});
socket.on('exchange', function(data){
console.log('exchange', data);
data.from = socket.id;
var to = io.sockets.connected[data.to];
to.emit('exchange', data);
});
socket.on("count", function(roomId, callback) {
var socketIds = socketIdsInRoom(roomId);
callback(socketIds.length);
});
});