-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiscord.js
74 lines (60 loc) · 2.18 KB
/
discord.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
72
73
const mongoose = require('mongoose');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
const db = require('./config/keys').mongoURI;
const users = require('./routes/api/users');
const servers = require('./routes/api/servers');
const channels = require('./routes/api/channels');
const messages = require('./routes/api/messages');
const passport = require('passport');
require('./config/passport')(passport);
mongoose
.connect(db)
.then(() => console.log('connected to mongodb successfully'))
.catch(err => console.log(err));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use("/api/users", users);
app.use("/api/servers", servers);
// servers.use("/:serverId/channels", channels);
app.use("/api/channels", channels);
app.use("/api/messages", messages);
app.use(passport.initialize());
const path = require('path');
if (process.env.NODE_ENV === 'production') {
// app.use(express.static('frontend/public'));
app.use(express.static(path.join(__dirname, 'frontend/build')));
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, './frontend/build', 'index.html'));
});
// Express serve up index.html file if it doesn't recognize route
}
app.use(express.static(path.join(__dirname, 'frontend/build')));
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname, 'frontend/build', 'index.html'));
});
const port = process.env.PORT || 5000;
const server = http.listen(port, () => console.log(`Server is running on port ${port}`));
io.on('connection', function(socket) {
console.log('a user has connected');
socket.on('disconnect', function() {
console.log('a user has disconnected');
});
let room;
socket.on("JOIN_CHANNEL", data => {
console.log(`joined channel ${data.channelId}`);
room = data.channelId;
socket.join(room);
});
socket.on("LEAVE_CHANNEL", data => {
room = data.channelId;
socket.leave(room);
console.log(`left channel ${data.channelId}`);
});
socket.on('SEND_MESSAGE', function(data) {
io.to(room).emit('RECEIVE_MESSAGE', data);
});
});