-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
62 lines (47 loc) · 1.44 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
const express = require('express');
const socket = require('socket.io');
const app = express();
var server = app.listen(4000, function () {
console.log("server running");
});
app.use(express.static("public"));
var io = socket(server);
io.on('connection', function (socket) {
console.log("User connected: " + socket.id);
socket.on('join', function (roomName) {
// will give us whole list of sockets/rooms
var rooms = io.sockets.adapter.rooms;
var room = rooms.get(roomName); //get our curr room
// if room doesnt exist, create
if (room == undefined) {
socket.join(roomName);
socket.emit("created");
}
// if room has one person, join room
else if (room.size == 1) {
socket.join(roomName);
socket.emit("joined");
}
// else means two users joined, full
else {
socket.emit("full");
}
console.log(rooms);
});
socket.on('ready', function (roomName) {
console.log("ready");
socket.broadcast.to(roomName).emit('ready');
});
socket.on('candidate', function (candidate, roomName) {
console.log("candidate");
socket.broadcast.to(roomName).emit('candidate', candidate);
});
socket.on('offer', function (offer, roomName) {
console.log("offer");
socket.broadcast.to(roomName).emit('offer', offer);
});
socket.on('answer', function (answer, roomName) {
console.log("answer");
socket.broadcast.to(roomName).emit('answer', answer);
});
});