-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
45 lines (37 loc) · 1.14 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
const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const path = require('path');
//Init the express app
const app = express();
const port = process.env.PORT;
// const port = 3000;
//Setup http server and bind it to socket.io
const httpServer = http.createServer(app);
const io = socketIO.listen(httpServer);
//Set public paths
// app.use("/styles", express.static(path.join(__dirname,'styles')));
app.use(express.static(__dirname + '/public'));
//Defining endpoints
//Set home endpoint
app.get('/', (req, res) => {
//Send HTML file as homepage
res.sendFile(__dirname + '/index.html');
});
io.on('connection', (socket) => {
console.log('A user connected');
//Handle new Chat Message
socket.on('chat message', (msg) => {
console.log(msg.username);
console.log('new message : ' + msg.message);
socket.broadcast.emit('chat message', msg);
});
//Handle disconnect
socket.on('disconnect', () => {
console.log('User disconnected');
})
});
//Open up the server
httpServer.listen(port, () => {
console.log("App is listening on port: " + port);
})