-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
36 lines (28 loc) · 907 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
const express = require('express')
const http = require('http')
const socketio = require('socket.io')
const path = require('path')
const ejs = require('ejs')
const app = express()
const server = http.createServer(app)
const io = socketio(server)
const messages = []
app.use(express.static(path.join(__dirname, 'public')))
app.set('views', path.join(__dirname, 'public'))
app.engine('html', ejs.renderFile)
app.set('view engine', 'html')
app.get('/', (request, response) => {
response.render('index.html')
})
io.on('connection', socket => {
console.log(`User login: ${socket.id}`)
socket.emit('previousMessages', messages)
socket.on('sendMessage', (message) => {
messages.push(message)
socket.broadcast.emit('recivedMessage', message)
})
})
const port = process.env.PORT || 3000;
server.listen(port, () => {
console.log(`> Server listening on port ${port}`)
})