-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
90 lines (72 loc) · 2.83 KB
/
server.js
File metadata and controls
90 lines (72 loc) · 2.83 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
express = require('express')
const app = express()
const server = require('http').Server(app)
const { SocketAddress } = require('net')
const {v4: uuid} = require('uuid') //will do the job of generating room uuid so that many people may use the same port
const {ExpressPeerServer} = require('peer')
const p2pserver = ExpressPeerServer(server,{
debug: true
})
app.set('view engine','ejs')
app.use('/peerjs',p2pserver)
app.use(express.static('public')) //serving the files from the server kyuki kaun script leke baithega
const io = require('socket.io')(server, {
cors: {
origin: '*',
}
})
app.get('/',(req, res)=>
{
res.redirect(`/${uuid()}`) //redirect user to random room and then that giy can share link may implement in front end
})
let tempr
app.get('/:roomuuid',(req,res)=>
{
tempr=req.params.roomuuid
//console.log(`hi ${req.params.roomuuid}`) checked uuid works refer docs once more before submission
res.render('room', {room_uuid : req.params.roomuuid}) //gotta pass the room id to the front end
})
// A note for maintaining my sanity in future
// So basicallly what is happening over here is that the momemnt the user is redirected to the room we are making
// that id available in the front end logic via ejs and then we generate the event that a new user just joined
// the room and here we wait for the socket connection and the backend server is just there for mainly facilitating
// these connections
const peers ={} //attempt to fix static stream frame bug on D/C or closing the tab
const sockmap={}
io.on('connection',(socket)=>
{
socket.on('newuserjoined',(roomuuid,pid,username)=>
{
//console.log("Testsocket")
//console.log("new user with " + username)
// console.log(socket.id)
users[socket.id]=username
//console.log(pid)
peers[socket.id]=pid
sockmap[socket.id]=roomuuid
//console.log(peers[socket.id])
socket.join(roomuuid)
socket.broadcast.to(roomuuid).emit('joinevent',pid,username) //https://socket.io/docs/v3/rooms/index.html docs for future reference
})
socket.on('send',(roomuuid,username,messagesent)=>
{
//console.log('Message recieved by the server' + messagesent)
socket.broadcast.to(roomuuid).emit('incoming',`${username} : ${messagesent}`,username)
})
socket.on('disconnect',(socid)=>
{
// console.log(socket.id)
// console.log(peers[socket.id])
//console.log(socket.id)
//socket.broadcast.to(sockmap[socket.id]).emit('ul',users[socket.id],peers[socket.id])
//above line can be uncommented to test is buggy
delete users[socket.id]
})
socket.on('userl',(temp,roomuuid,pidl)=>
{
socket.broadcast.to(roomuuid).emit('ul',temp,pidl)
})
})
//chat ki koshish
const users={}
server.listen(process.env.PORT||5000)