-
Notifications
You must be signed in to change notification settings - Fork 6
/
server.js
105 lines (89 loc) · 3.68 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const express = require("express");
const cors = require("cors");
const db = require("./db/db");
const socket = require('socket.io')
const app = express();
app.use(cors());
app.use(express.json());
const PORT = process.env.PORT || 5000;
const bodyParser = require("body-parser")
app.use(bodyParser.urlencoded({ extended: true }))
const path = require('path');
app.use(express.static(path.resolve(__dirname, './client/build')));
// import Routers
const signupRouter = require("./routers/routes/auth/signup")
const fundraiserRouter = require("./routers/routes/fundraiser")
const loginRouter = require("./routers/routes/auth/login")
const adminRouter = require("./routers/routes/AdminFuction")
const bloodpostRouter = require("./routers/routes/bloodPost")
const searchRouter = require('./routers/routes/Services/search')
const contributionRouter = require('./routers/routes/Contribution')
const stripeRouter = require('./routers/routes/Services/stripe')
const helpSpecificRouter = require('./routers/routes/helpSpecific')
const { join_User, get_Current_User, user_Disconnect, c_users } = require('./routers/controllers/Services/chat');
const forgetPasswordRouter = require('./routers/routes/Services/forgotPassword')
// const { isTypedArray } = require("util/types");
// const {postInformationSender} = requrire bla bla //
//app Routers
app.use("/signup", signupRouter);
app.use("/fundraiser", fundraiserRouter)
app.use("/login", loginRouter)
app.use('/admin', adminRouter);
app.use('/bloodpost', bloodpostRouter);
app.use('/search', searchRouter);
app.use("/Contribution", contributionRouter);
app.use('/payment', stripeRouter)
app.use('/helpSpecific', helpSpecificRouter)
app.use('/forgetPassword', forgetPasswordRouter)
app.use("*", (req, res) => res.status(404).json("NO content at this path"));
const server = app.listen(PORT, () => {
console.log(`Server On ${PORT}`);
});
const io = socket(server, { cors: { origin: "*" } })
app.set('socketio', io); // exporting socket io to all files - global ussage in all app files
io.on("connection", (socket) => {
//for a new user joining the room
socket.on("joinRoom", ({ username, roomname }) => {
//* create user
const p_user = join_User(socket.id, username, roomname);
console.log(socket.id, "=id");
socket.join(p_user.room);
//display a welcome message to the user who have joined a room
socket.emit("message", {
userId: p_user.id,
username: p_user.username,
text: `Welcome ${p_user.username}`,
});
//displays a joined room message to all other room users except that particular user
socket.broadcast.to(p_user.room).emit("message", {
userId: p_user.id,
username: p_user.username,
text: `${p_user.username} has joined the chat`,
});
io.emit("admin",{allusers:c_users})
});
//user sending message
socket.on("chat", (text) => {
//gets the room user and the message sent
const p_user = get_Current_User(socket.id);
io.to(p_user.room).emit("message", {
userId: p_user.id,
username: p_user.username,
text: text,
});
});
//when the user exits the room
socket.on("disconnectOne", (room) => {
//the user is deleted from array of users and a left room message displayed
const p_user = user_Disconnect(socket.id);
socket.leave(p_user.room)
if (p_user) {
io.to(p_user.room).emit("message", {
userId: p_user.id,
username: p_user.username,
text: `${p_user.username} has left the room`,
});
}
});
}); //end socket
module.exports = {io}