-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
71 lines (54 loc) · 1.98 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
59
60
61
62
63
64
65
66
67
68
69
70
71
const express = require('express');
const path = require('path');
const logger = require('morgan');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const { userEnter, userLeave, currentUser, usersInRoom } = require('./src/utils/users')
// const router = require('./routes/index')
app.use(logger('dev'));
app.use(express.json());
// app.use(router);
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'index.html'));
});
io.on('connection', (socket) => {
console.log('according to this data i am much stronger than ' + socket.id);
socket.on('join', ({ username, room }) => {
const { user } = userEnter({ id: socket.id, username, room });
socket.join(user.room);
socket.emit('message', {
user: 'SocketChat',
text: `Welcome to ${user.room}, ${user.username}!`
});
socket.broadcast.to(user.room).emit('message', {
user: 'SocketChat',
text: `${user.username} has entered the chat.`
});
io.to(user.room).emit('roomUsers', { room: user.room, users: usersInRoom(user.room) });
});
socket.on('sendMessage', msg => {
const user = currentUser(socket.id);
io.to(user.room).emit('message', {
user: user.username,
text: msg
});
});
socket.on('disconnect', () => {
console.log('user disconnected!')
const user = userLeave(socket.id);
if (user) {
io.to(user.room).emit('message', {
user: 'SocketChat',
text: `${user.username} has left the chat.`
});
io.to(user.room).emit('roomUsers', {
room: user.room,
users: usersInRoom(user.room)
});
};
});
});
const port = process.env.PORT || 3001;
server.listen(port, () => console.log(`Server running on port ${port}`));