-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cjs
84 lines (77 loc) · 2.91 KB
/
server.cjs
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
const WebSocket = require('ws');
const http = require('http');
const clients = new Map();
function parseCookies(cookieHeader) {
const cookies = {};
if (cookieHeader) {
const cookieStrings = cookieHeader.split(';');
for (const cookieString of cookieStrings) {
const [name, value] = cookieString.trim().split('=');
cookies[name] = decodeURIComponent(value);
}
}
return cookies;
}
const server = http.createServer((req, res) => {
console.log(clients)
const cookies = parseCookies(req.headers.cookie)
console.log(cookies)
if(cookies.access){
console.log("access granted")
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws) => {
console.log('Client connected');
const {createConnection} = require('mysql2');
const con = createConnection({
host: 'localhost',
user: 'root',
password: 'l',
database: 'messaging_app',
})
let senderid;
ws.on('message', async (message) => {
console.log(`Received message: ${message}`);
let {sender,receiver} = JSON.parse(message);
senderid = sender;
clients.set(sender,ws);
for(let [key, value] of clients){
value.send(JSON.stringify({type:"online",data:Array.from(clients.keys())}))
}
const query = `
SELECT *
FROM Conversation
WHERE (Sender = ? AND Receiver = ?) OR (Sender = ? AND Receiver = ?)
ORDER BY CId
`;
const result = await con.promise().execute(query,[sender,receiver,receiver,sender]).then((res)=>{return res[0]});
ws.send(JSON.stringify(result));
console.log("SENT MESSAGE TO SENDER")
console.log(clients.has(receiver));
if (clients.get(receiver)){
let wsss = clients.get(receiver);
console.log(wsss);
wsss.send(JSON.stringify(result));
console.log("SENT MESSAGE TO RECEIVER")
}
console.log(clients.keys())}
);
ws.on('close', () => {
con.end();
console.log('Client disconnected');
clients.delete(senderid);
});
});
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('HTTP server works!\n');
}else{
res.writeHead(403, { 'Content-Type': 'text/plain' });
res.end('Access denied\n');
}
});
setInterval(()=>{console.log(clients.keys())},1000);
server.listen(9000);
// const server = new WebSocket.Server({ port: 9000 });
//
//
// // });
//