-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (49 loc) · 2.05 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
const path = require('path');
const http = require('http');
const express = require('express');
const socketio = require('socket.io');
const db = require('./db');
const formatMessage = require('./utils/messageObject');
const {userJoinsRoom, getCurrentUser, userLeavesRoom, getRoomUsers} = require('./utils/userHelperFunctions');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
// set static folder
app.use(express.static(path.join(__dirname, 'public')));
// run when client connects
io.on('connection', socket => {
console.log('New web socket connection.');
socket.on('joinRoom', ({username, room}) => {
const user = userJoinsRoom(socket.id, username, room);
socket.join(user.room);
// welcomes current user
socket.emit('message', formatMessage("Server message: ", 'Welcome to the chat!'));
// broadcast when a user connects
// broadcast emits to every client except the one who has connected
socket.broadcast.to(user.room).emit('message', formatMessage("Server message: ", `${user.username} has joined the chat.`));
// send users and room info
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
});
// listen for chat message
socket.on('chatMessage', (msg) => {
const user = getCurrentUser(socket.id);
io.to(user.room).emit('message', formatMessage(user.username, msg));
});
// run when client disconnects
socket.on('disconnect', () => {
const user = userLeavesRoom(socket.id);
if(user) {
io.to(user.room).emit('message', formatMessage("Server message: ", `${user.username} has left the chat.`));
// send users and room info
io.to(user.room).emit('roomUsers', {
room: user.room,
users: getRoomUsers(user.room)
});
}
});
});
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));