-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
58 lines (44 loc) · 1.63 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const path = require("path");
const http = require("http");
const express = require("express");
const socketio = require("socket.io");
const formatMessage = require('./utils/messages');
const {
joinUser,
getCurrentUser,
userLeft,
getRoomUsers
} = require('./utils/users')
const app = express();
const server = http.createServer(app);
const io = socketio(server);
const port = process.env.PORT || 3000;
const appName = process.env.APPNAME || "ChatApp";
app.use(express.static(path.join(__dirname, 'public')));
io.on('connection', (socket) => {
socket.on('chatMessage', (msg) => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit('message', formatMessage(user.name,msg));
});
socket.on('joinRoom', ({username, room}) => {
const user = joinUser(socket.id, username, room);
socket.join(user.room);
socket.emit('message', formatMessage(appName, 'Welcome to ChatApp'));
socket.broadcast.to(user.room).emit('message', formatMessage(appName, `${user.name} has joined the room.`));
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
});
socket.on('disconnect', () => {
const user = userLeft(socket.id);
if(user){
io.to(user.room).emit('message', formatMessage(appName, `${user.name} left the room.`));
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
}
});
});
server.listen(port, () => console.log(`[Chat-App Server]: Server running on port <${port}>.`))