-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
68 lines (46 loc) · 1.97 KB
/
server.js
File metadata and controls
68 lines (46 loc) · 1.97 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
66
67
const path = require('path');
const http = require('http');
const express = require('express');
const socketio = require('socket.io');
const messages = require('./utils/messages.js');
const users = require('./utils/users.js');
const cors = require('cors');
const app = express();
app.use(cors());
const server = http.createServer(app);
const io = socketio(server);
const botName = 'Admin Bot';
//Set static folder
app.use(express.static(path.join(__dirname,'public')));
app.use(function(req,res,next){ res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Methods", "GET, PUT, POST, DELETE, PATCH"); res.header("Access-Control-Allow-Headers", "Accept, Content-Type, Authorization, X-Requested-With"); next(); });
//Run when Client connects
io.on('connection', socket => {
socket.on('joinRoom', (username) =>{
const user = users.userJoin(socket.id, username.username);
//nur an speziellen Client
socket.emit('message', messages.formatMessage(botName, 'Wilkommen im Gruppenchat' ));
//an alle außer speziellen Client
console.log(user);
socket.broadcast.emit('message', messages.formatMessage(botName, `${user.username} ist dem Gruppenchat beigetreten`));
//Nutzer senden
io.emit('users', users.getUsers());
});
//an alle
//io.emit()
//Auf chatMessage warten
socket.on('chatMessage', (msg) => {
const user = users.getCurrentUser(socket.id);
io.emit('message', messages.formatMessage(user.username, msg));
})
//Wenn Client Verbindung trennt
socket.on('disconnect', () => {
const user = users.userLeave(socket.id);
if(user){
io.emit('message', messages.formatMessage(botName, `${user.username} hat den Gruppenchat verlassen`));
}
//Nutzer senden
io.emit('users', users.getUsers());
})
});
const PORT = 3000 || process.env.PORT;
server.listen(PORT, () => console.log(`Server running on port ${PORT}`));