-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
85 lines (62 loc) · 2.37 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
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
const express = require('express');
const app = express();
const { ExpressPeerServer } = require('peer');
const { v4: uuidv4 } = require('uuid');
const server = require('http').Server(app);
const io = require('socket.io')(server);
const main = async () => {
// add peer server
const peerServer = ExpressPeerServer(server, {
debug: true,
path: '/peerjs'
});
// set app view engine
app.set('view engine', 'ejs');
// load the files in "public directory"
app.use(express.static('public'));
// use peer js server
// app.use(peerServer);
app.use(peerServer);
// define express routes
app.get("/", (req, res) => {
const roomId = uuidv4();
res.redirect(`/${roomId}`);
});
app.get("/:room", (req, res) => {
res.render('room', { roomId: req.params.room });
});
// listen to socket connections
io.on('connection', (socket) => {
socket.on('join-room', (data) => {
const { roomID, peerUserId } = data;
if (roomID) {
// register the socket to the a channel
socket.join(roomID); // define roomID as channel to register to
console.log(' a user connected : ', data);
// emmit a brodcast to the channel.
socket.to(roomID).emit("user-connected", peerUserId);
console.log(' emit user connected passed peerUserId : ', peerUserId, " passing roomID as :", roomID);
}
});
socket.on('message', (data) => {
const { ROOM_ID, message } = data;
if (ROOM_ID) {
// emmit a brodcast to the channel.
socket.to(ROOM_ID).broadcast.emit("new-message", { message });
}
});
socket.on('screen-share-init', (data) => {
const { roomID, peerUserId } = data;
if (roomID && peerUserId) {
// emmit a brodcast to the channel.
socket.to(roomID).broadcast.emit("user-screen-share", { roomID, peerUserId });
}
});
// catch socket error before it catches us unaware
socket.on('error', (reason) => {
console.log(reason); // prints the message associated with the error, e.g. "thou shall not pass" in the example above
});
});
server.listen(3030);
}
main().catch(e => console.log(e));