-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
112 lines (97 loc) · 3.04 KB
/
index.js
File metadata and controls
112 lines (97 loc) · 3.04 KB
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
"use strict";
var os = require("os");
var nodeStatic = require("node-static");
var http = require("http");
var socketIO = require("socket.io");
var fileServer = new nodeStatic.Server();
var app = http
.createServer(function(req, res) {
fileServer.serve(req, res);
})
.listen(8080, function() {
console.log("Sever started on port : 8080");
});
var io = socketIO.listen(app);
let sockets = {};
io.sockets.on("connection", function(socket) {
// convenience function to log server messages on the client
console.log("total users", Object.keys(io.sockets.sockets).length);
function log() {
var array = ["Message from server:"];
array.push.apply(array, arguments);
socket.emit("log", array);
}
socket.on("username", username => {
socket.username = username;
sockets[username] = socket;
});
socket.on("call", data => {
let { description, username } = data;
let remoteUserSocket = sockets[username];
if (remoteUserSocket) {
remoteUserSocket.emit("incomingCall", {
description,
username: socket.username
});
} else {
log("user not connected");
}
});
socket.on("addCandidate", iceCandidate => {
if (sockets[iceCandidate.username])
sockets[iceCandidate.username].emit("remoteICECandidate", iceCandidate);
else console.log("no user to send ice data", sockets);
});
socket.on("acceptCall", data => {
let { description, username } = data;
let remoteUserSocket = sockets[username];
if (remoteUserSocket) {
remoteUserSocket.emit("callAnswered", {
description,
username: socket.username
});
} else {
log("user not connected");
}
});
socket.on("message", function(message) {
log("Client said: ", message);
// for a real app, would be room-only (not broadcast)
socket.broadcast.emit("message", message);
});
socket.on("create or join", function(room) {
log("Received request to create or join room " + room);
var clientsInRoom = io.sockets.adapter.rooms[room];
var numClients = clientsInRoom
? Object.keys(clientsInRoom.sockets).length
: 0;
log("Room " + room + " now has " + numClients + " client(s)");
if (numClients === 0) {
socket.join(room);
log("Client ID " + socket.id + " created room " + room);
socket.emit("created", room, socket.id);
} else if (numClients === 1) {
log("Client ID " + socket.id + " joined room " + room);
io.sockets.in(room).emit("join", room);
socket.join(room);
socket.emit("joined", room, socket.id);
io.sockets.in(room).emit("ready");
} else {
// max two clients
socket.emit("full", room);
}
});
socket.on("ipaddr", function() {
var ifaces = os.networkInterfaces();
for (var dev in ifaces) {
ifaces[dev].forEach(function(details) {
if (details.family === "IPv4" && details.address !== "127.0.0.1") {
socket.emit("ipaddr", details.address);
}
});
}
});
socket.on("bye", function() {
console.log("received bye");
});
});