-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
150 lines (110 loc) · 3.79 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const express = require("express");
const app = express();
const bodyParser = require('body-parser')
var morgan = require('morgan')
var mongoose = require('mongoose')
var cors = require('cors')
var path = require('path')
var UserRoutes = require('./backend/Routes/UserRoutes')
var chatRoutes = require('./backend/Routes/ChatRoutes')
const config = require("./backend/config/key");
const PORT = process.env.PORT || 4000
mongoose.connect(config.mongoURI,
{
useNewUrlParser: true, useUnifiedTopology: true,
useCreateIndex: true, useFindAndModify: false
})
.then(() => console.log('MongoDB Connected...'))
.catch(err => console.log(err));
// parse application/json
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json())
app.use(morgan('dev'))
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Credentials", "true")
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept,Authorization"
);
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,PATCH,OPTIONS');
next();
});
// app.use(cors({
// origin: [
// "http://localhost:4200"
// ], credentials: true
// }));
const server = require('http').Server(app);
const io = require('socket.io')(server);
var users = []
io.on('connection', (socket) => {
const userId = socket.request._query['userId']
// users= users.filter(user => user.userId != userId)
socket.join(userId)
users.push({ socketId: socket.id, userId: userId })
console.log('user connected = ', socket.id + " " + userId);
io.emit('newUser', users)
socket.on('typing', (receiver, sender) => {
// const a = users.find(user => user.userId == receiver)
// if (a)
// io.to(a.socketId).emit('typing', sender)
socket.to(receiver).emit('typing' , sender)
})
socket.on('join', (data) => {
console.log(`Requesting ${data.toID} to join request by ${data.fromID}`)
const a = users.find(user => user.userId == data.toID)
socket.to(data.toID).emit('request', {fromID :data.fromID , toID : data.toID ,fromName:data.fromName})
})
socket.on('start_call', (data) => {
console.log(`${data.toID} acceptrd your request to join call`)
socket.to(data.fromID).emit('start_call',data)
})
socket.on('webrtc_offer', (data) => {
console.log(`webrtc_offer event to peer ${data.toID}`)
socket.to(data.toID).emit('webrtc_offer', data)
})
socket.on("reject-call", data => {
socket.to(data.fromID).emit("call-rejected", {
fromID: data.fromID,
toID : data.toID,
});
})
socket.on('webrtc_answer', (data) => {
console.log(`webrtc_answer event to peer ${data.fromID}`)
socket.to(data.fromID).emit('webrtc_answer', data)
})
socket.on('webrtc_ice_candidate', (data) => {
console.log(`Broadcasting webrtc_ice_candidate event to peer ${data.toID}`)
socket.to(data.toID).emit('webrtc_ice_candidate', data)
})
socket.on('busy', (data)=>{
socket.to(data.fromID).emit('busy',data)
})
socket.on('over',data=>{
socket.to(data.toID).emit('over',data)
})
socket.on('disconnect', () => {
users = users.filter(user => user.socketId != socket.id)
console.log('user disconnected = ', socket.id);
// socket.leave(userId)
io.emit('newUser', users)
});
io.clients((err, clients) => {
console.log(clients);
})
});
app.use((req, res, next) => {
req.io = io;
req.users = users
next();
});
app.use('/api/user', UserRoutes)
app.use('/api/chat', chatRoutes)
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'dist/chat')))
app.get('/*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist/chat', 'index.html'))
})
}
server.listen(PORT, () => console.log(`Server started on ${PORT}`))