-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
76 lines (59 loc) · 2.1 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require('dotenv').config();
const express = require('express');
const socket = require('socket.io');
const messageFormatter = require('./utils/messageFormat');
const morgan = require('morgan');
const {createClient} = require('redis');
const createAdapter = require("@socket.io/redis-adapter").createAdapter;
const { getCurrentUser, userJoin, getRoomUsers,userLeave } = require('./utils/users');
const app = express();
app.use(express.static('./view'));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(morgan('dev'));
// (async () => {
// pubClient = createClient({ url: "redis://127.0.0.1:5500" });
// await pubClient.connect();
// subClient = pubClient.duplicate();
// io.adapter(createAdapter(pubClient, subClient));
// })();
const server = require('http').createServer(app);
const io = socket(server);
let displayRoamInfo = (user) => {
io.to(user.room).emit("roomUsers", {
room: user.room,
users: getRoomUsers(user.room),
});
}
let number = 0 ;
io.on('connection', (socket) => {
number++;
console.log('user ' + socket.id + ' connected');
console.log(number + ' users connected');
socket.on('joinRoom', ({ username, room }) => {
const user = userJoin(socket.id, username, room);
socket.join(user.room);
socket.broadcast.to(user.room).emit('message', messageFormatter('Admin', `${user.username} has joined the chat`));
socket.on('chatMessage', (msg) => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit('message', messageFormatter(user.username, msg));
}
);
displayRoamInfo(user);
socket.on('disconnect', () => {
const user = userLeave(socket.id);
if (user) {
io.to(user.room).emit('message', messageFormatter('Admin', `${user.username} has left the chat`));
}
displayRoamInfo(user);
}
);
}
);
}
);
const port = process.env.PORT || 5000;
server.listen(port, () => {
console.log('Server is running on http://localhost:' + port);
}
);