forked from TiagoDanin/RealTimeChatAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
46 lines (36 loc) · 1004 Bytes
/
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
const express = require('express')
const helmet = require('helmet')
const path = require('path')
const {
v4: uuidv4
} = require('uuid')
const http = require('http')
const app = express()
const server = http.createServer(app)
const io = require('socket.io')(server)
const port = process.env.PORT || 8000
const build = 1
app.use(helmet.dnsPrefetchControl())
app.use(helmet.hidePoweredBy())
app.use(helmet.permittedCrossDomainPolicies())
app.use(helmet.xssFilter())
app.set('trust proxy', 1)
app.use(express.static(path.join(__dirname, 'public')))
io.on('connection', socket => {
console.log('[!] Join')
socket.on('disconnect', () => {
console.log('[!] Disconnect')
})
socket.on('sendMessage', message => {
const {
content
} = message
const messageId = uuidv4()
console.log('[+] sendMessage:', content, messageId)
socket.broadcast.emit('receiveMessage', {
content,
messageId
})
})
})
server.listen(port, () => console.log(`App listening at http://localhost:${port}`))