-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
66 lines (46 loc) · 1.84 KB
/
server.js
File metadata and controls
66 lines (46 loc) · 1.84 KB
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
const express = require('express');
const path = require('path');
const http = require('http');
const socketio = require ('socket.io')
const formatMsg = require('./utils/msg');
const {uJoin, getCurrentUser, leave, getAllCurrentUsers}= require('./utils/users');
const app = express();
const server = http.createServer(app);
const io = socketio(server);
// Set a static folder
app.use(express.static(path.join (__dirname, 'Chatroom')));
// Run when user connects
io.on ('connection', socket => {
socket.on('joinRoom', ({username}) => {
const user = uJoin(socket.id, username)
socket.join();
// welcomes the user when they join
socket.emit ('message', formatMsg('Bot', 'Welcome to the Chat Room'));
//shows when a user connects
socket.broadcast.emit('message', formatMsg('Bot',`${user.name} has joined the chat`));
//send users
io.emit('allUsers', {users: getAllCurrentUsers()});
});
//list for chatMessage
socket.on ('chatMessage', msg => {
const user = getCurrentUser(socket.id)
if (msg.length <129){
io.emit ('message', formatMsg(user.name, msg));
}else{
io.to(socket.id).emit ('message', formatMsg ('Bot', "Error: You're message exceed 128 characters"));
}
});
//when user disconnects
socket.on('disconnect', () => {
const user = leave(socket.id);
if (user) {
io.emit(
'message',
formatMsg('Bot', `${user.name} has left the chat`));
//send users
io.emit('allUsers', {users: getAllCurrentUsers()});
}
});
});
const PORT = 2000 || process.env.PORT;
server.listen (PORT, () => console.log('Server running on port ${PORT}'));