-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths-copy.js
96 lines (84 loc) · 2.26 KB
/
s-copy.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
const express = require("express");
const app = express();
const server = require("http").Server(app);
const io = require("socket.io")(server);
/**
*
* user peer JS live communication b/w diff clients browser
*
* */
const { ExpressPeerServer } = require("peer");
const peerServer = ExpressPeerServer(server, {
debug: true,
});
/**
*
* now tell peer server which url we use
*
* */
app.use("/peerjs", peerServer);
/**
*
* specific way into import uuid version 4.
*
* */
const { v4: uuidv4 } = require("uuid");
/**
*
* example of uuidv4 badaf671-9dc7-4a79-9544-01f72e4ca78f
* */
app.set("view engine", "ejs");
/**
* tell the server is about public folder for the project
*
*/
app.use(express.static("public"));
app.get("/", (req, res) => {
/**
*
* when root url is hit , it auto redirect them to room url with a unique ID
*
* */
res.redirect(`/${uuidv4()}`);
});
/**
*
* :room is a parameter ,
* pass JS variable to html or .ejs file
*
* */
app.get("/:room", (req, res) => {
res.render("room", { roomID: req.params.room });
});
io.on("connection", (socket) => {
socket.on("join-room", (roomID, userConnID) => {
console.log("jjjjjj");
console.log("joinned room , with Room ID ---- ", roomID);
socket.join(roomID);
// this tell that someone is connected to room
// socket.to(roomID).broadcast.emit("user-connected");
// socket.broadcast.to(roomID).emit("user-connected");
// socket.to(roomID);
socket.broadcast.to(roomID).emit("user-connected", userConnID);
// only emit the funtion
// socket.emit("user-connected");
/**
* when a user is connected we also make a recevier which can recveird new or previous user messages
*
* */
socket.on("message", (message) => {
// now emit this message to script file where the actuall receiver is & that is resp to display msg's
io.to(roomID).emit("createMessage", message);
});
});
});
server.listen(process.env.PORT || 3030);
// const fs = require("fs");
// const path = require("path");
// const roomJsFile = path.join(__dirname, "views/room.ejs");
// fs.readFile(roomJsFile, "utf8", (err, data) => {
// if (err) throw err;
// res.writeHead(200, { "content-type": "text/html" });
// res.write(data);
// res.end();
// });