-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
134 lines (95 loc) · 3.41 KB
/
index.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
const express = require('express');
const connectToMongo = require('./db');
const auth = require('./routes/auth');
const message = require('./routes/message');
const cors = require('cors');
const http = require('http');
const { Server } = require('socket.io');
const jwt = require('jsonwebtoken');
// const fetch = require('node-fetch')
const JWT_SECRET = "AbhiIsAVerySexyB$oy";
const app = express();
connectToMongo();
app.use(cors());
app.use(express.json());
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: "http://localhost:3000",
methods: ["GET", "POST", "PUT", "PATCH", "DELETE"]
},
});
io.on('connection', (socket) => {
let currentRoomId;
console.log(`A User with the id: ${socket.id} connected!!`);
socket.on('sent_message', (sent_data) => {
socket.to(sent_data.by + sent_data.to).emit('received_message', sent_data);
})
socket.on('room_changed', (payload) => {
io.sockets.adapter.rooms.forEach((room) => {console.log(room);});
socket.leaveAll();
socket.join(payload.user_Id);
socket.join(payload.id + payload.user_Id);
})
socket.on('typing', (payload) => {
socket.to(payload.by + payload.to).emit('heIsTyping', payload);
})
socket.on('leave_room', (id) => {
socket.leave(id);
});
socket.on('sent_fr_notification', (id) => {
// console.log('Notification Sent');
socket.to(id).emit('notification_received');
})
socket.on('userloggedin', (user_Id) => {
socket.join(user_Id);
currentRoomId = user_Id;
})
socket.on('sent_chat_notification', ({ id, user_Id }) => {
socket.to(id).emit('chat_notification_received', user_Id);
})
socket.on('content_changed_refresh_discussions', (user_Id) => {
socket.to(user_Id).emit('refresh_discussions')
})
socket.on('isOnline', (user_Id) => {
console.log("Is this the fuck??");
socket.broadcast.emit('cameOnline', user_Id);
})
socket.on('disconnecting', () => {
socket.to(currentRoomId).emit('goOffine')
})
socket.on('disconnect', () => {
// const goingOffline = async () => {
// const payload = {
// user: {
// id: currentRoomId
// }
// }
// // Signing the JWT(START)
// const token = jwt.sign(payload, JWT_SECRET);
// // Signing the JWT(END)
// const response = await fetch(`http://localhost:5000/api/auth/offline`, {
// method: 'PUT',
// headers: {
// 'Content-Type': 'application/json',
// 'auth-token': token // Dude, this line fucking took me 10 mins to identify that i've not added it lol
// },
// });
// const json = await response.json();
// console.log(json);
// }
// goingOffline();
socket.broadcast.emit('goingOffline', currentRoomId);
console.log(`User with the id: ${currentRoomId} disconnected`);
})
})
app.use('/api/auth', auth);
app.use('/api/message', message);
app.get('/', (req, res) => {
res.send('ok!')
console.log("ok")
});
const port = 5000;
server.listen(port, () => {
console.log(`Server Started successfully at port ${port}`)
})