-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
38 lines (29 loc) · 1.07 KB
/
server.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
const express = require('express');
const exphbs = require('express-handlebars');
const path = require("path");
const sio = require('socket.io')
const app = express();
app.engine('handlebars', exphbs());
app.set('view engine', 'handlebars');
const PORT = 8000;
app.use(express.json());
app.use(express.urlencoded());
app.use(require('./routes/htmlRoutes'));
app.use(require('./routes/api'));
app.use(express.static(path.join(__dirname, 'public')));
const io = sio.listen(
app.listen(process.env.PORT || PORT, () => {
console.log(`server running on ${process.env.PORT || PORT}`);
}));
io.sockets.on('connection', function (socket) {
socket.on('username', function (username) {
socket.username = username;
io.emit('is_online', '🔵 <i>' + socket.username + ' join the chat..</i>');
});
socket.on('disconnect', function (username) {
io.emit('is_online', '🔴 <i>' + socket.username + ' left the chat..</i>');
})
socket.on('chat_message', function (message) {
io.emit('chat_message', '<strong>' + socket.username + '</strong>: ' + message);
});
});