-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
38 lines (31 loc) · 1.03 KB
/
server.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
const express = require("express");
const http = require("http");
const app = express();
const server = http.createServer(app);
const socket = require("socket.io");
const io = socket(server);
const rooms = {};
io.on("connection", socket => {
socket.on("join room", roomID => {
if (rooms[roomID]) {
rooms[roomID].push(socket.id);
} else {
rooms[roomID] = [socket.id];
}
const otherUser = rooms[roomID].find(id => id !== socket.id);
if (otherUser) {
socket.emit("other user", otherUser);
socket.to(otherUser).emit("user joined", socket.id);
}
});
socket.on("offer", payload => {
io.to(payload.target).emit("offer", payload);
});
socket.on("answer", payload => {
io.to(payload.target).emit("answer", payload);
});
socket.on("ice-candidate", incoming => {
io.to(incoming.target).emit("ice-candidate", incoming.candidate);
});
});
server.listen(8000, () => console.log('server is running on port 8000'));