-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (47 loc) · 1.47 KB
/
index.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
const express = require('express');
const cors = require('cors');
const dotenv = require('dotenv');
const socket = require('socket.io')
const app = express();
const http = require('http');
const server = http.createServer(app);
const PORT = process.env.PORT || 4000
const mongoose = require("mongoose");
// const {registerUsers } = require('./controllers/signUpController');
const router = require('./routers');
app.use(express.urlencoded({extended:true}));
app.use(express.static(__dirname + '/asset'));
app.use(express.json());
app.use(cors());
app.use('/', router)
app.set('strictQuery', false);
dotenv.config();
mongoose.connect(process.env.URI).then(res =>{
console.log('Mongoose Db Connected');
}).catch(err =>{
console.log("Mongoose connection failed");
console.log(err);
})
// router.post('/register', registerUsers)
const serverIo = server.listen(PORT, (req, res) => {
console.log(`Server is listening at https://localhost:${PORT}`)
});
const io = socket(serverIo, {
cors:{
origin:"*",
Credential:true
}
})
global.onlineUers = new Map();
io.on('connection', (socket) => {
global.chatsocket = socket;
socket.on("addUser", (id) => {
onlineUers.set(id, socket.id);
});
socket.on("send-msg", (data) => {
const sendUserSocket = onlineUers.get(data.to);
if(sendUserSocket){
socket.to(sendUserSocket).emit("msg-receive", data.message);
}
})
})