-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
123 lines (90 loc) · 3.13 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
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
const express = require('express')
const http = require('http')
const socketIo = require('socket.io')
const cors = require('cors')
const morgan = require('morgan')
const mongoose = require('mongoose')
const jwt = require('jsonwebtoken') // Import jwt
const config = require('./src/config')
const authRoutes = require('./src/routes/authRoutes');
const userRoutes = require('./src/routes/userRoutes')
const Chat = require('./src/models/chats')
const e = require('express')
// Create a new Express application
const app = express()
// Create an HTTP server from the Express application
const server = http.createServer(app)
// Create a new instance of Socket.IO from the HTTP server
const io = socketIo(server)
//configuracion de middlewares
app.use(cors())
app.use(express.json())
app.use(morgan('dev'))
//configuracion de base de datos
mongoose.connect(config.mongoURI)
mongoose.connection.on('connected', () => {
console.log('Connected to mongo instance')
})
//configuracion de rutas
app.use('/auth', authRoutes)
app.use('/user', userRoutes)
app.get('/',(req, res) => {
res.sendFile(process.cwd() + '/client/home.html')
})
//configuracion de autenticacion y adminbistracion de usuarios
app.get('/login', (req, res) => {
res.sendFile(process.cwd() + '/client/login.html')
})
app.get('/auth', (req, res) => {
res.send('route auth')
})
app.get('/register', (req, res) => {
res.sendFile(process.cwd() + '/client/register.html')
})
app.get('/favicon.ico', (req, res) => {
res.sendFile(process.cwd() + '/client/favicon.ico')
})
io.on('connection', async (socket) => {
// Obtener el nombre de la sala
const sender = socket.handshake.auth.sender.email
const receiver = socket.handshake.auth.receiver.email
const room = sender + receiver
// Desconectar el socket de la sala
socket.on('disconnect', () => {
socket.leave(room)
console.log('user disconnected')
});
// Crear la sala si no existe
const roomExists = await Chat.findOne({ participants: { $all: [sender, receiver] } })
if (!roomExists) {
await Chat.create({ participants: [sender, receiver], messages: [] })
}else{
console.log('room exists')
}
// Unir el socket a la sala
socket.join(room)
console.log('user connected')
// Enviar mensaje
socket.on('send message', async (msg) => {
try {
const chat = await Chat.findOne({ participants: { $all: [sender, receiver] } })
chat.messages.push({text: msg, sender: sender})
await chat.save()
io.to(room).emit('chat messages', chat.messages)
} catch (err) {
console.log(err)
}
})
// Obtener mensajes
if (!socket.recovered) {
const chat = await Chat.findOne({ participants: { $all: [sender, receiver] } })
socket.emit('chat messages', chat.messages)
socket.recovered = true
}
});
// configuraciuon de chats
app.get('/chat/:id', (req, res) => {
res.sendFile(process.cwd() + '/client/chats.html')
})
//configuracion de puerto para el server
server.listen(config.PORT, () => console.log('Listening on port ' + config.PORT))