-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
45 lines (35 loc) · 1.04 KB
/
app.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
require('dotenv').config();
const path = require('path');
const express = require('express');
const PORT = process.env.PORT || 3000;
const app = express();
const webServer = require('http').createServer(app);
const io = require('socket.io')(webServer);
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'public'));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.get('/', (req, res) =>
{
var socketServerAddress = process.env.SOCKETSERVER || `http://localhost:${PORT}`
res.render('index.ejs',
{
socketServerAddress: socketServerAddress
});
});
let messages = [];
io.on('connection', socket =>
{
socket.emit('previousMessages', messages);
socket.on('sendMessage', (message) =>
{
if(messages.length > 40)
messages.shift()
messages.push(message);
socket.broadcast.emit('receivedMessage', message);
});
});
webServer.listen(PORT, () =>
{
console.log(`App is running on port: ${PORT}`)
});