-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
40 lines (35 loc) · 1.11 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
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 PORT = process.env.PORT || 5000
const users = {}
io.on('connection', socket => {
if (!users[socket.id]) {
users[socket.id] = socket.id
}
// emait al existing user to new user
io.sockets.emit('allUsers', users)
//pushes data to client side
socket.emit('yourID', { id: socket.id })
// the on function for handle a emit
socket.on('callUser', data => { //
// we got 3 informmation
// 1 .user to call
// 2. from , ( call comming from where )
// 3. data ( Video)
io.to(data.userToCall).emit('hey',{signal:data.signalData,from:data.from})
})
socket.on('acceptCall',data=>{
io.to(data.to).emit('callAccepted',data.signal)
})
socket.on('disconnect', () => {
delete users[socket.id]
socket.broadcast.emit('allUsers', users)
})
})
server.listen(PORT, () => {
console.log('Server running on port : ', PORT)
})