-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_chat.js
62 lines (55 loc) · 1.79 KB
/
io_chat.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
const crypto = require("crypto");
const redis = require("redis");
const redisClient = redis.createClient();
var roomId, userName;
module.exports = {
socketServer: (io) => {
io.on("connection", (socket) => {
socket.emit("info-request", `Requesting info for ${socket.id}`);
socket.on("info-response", (msg) => {
roomId = msg.roomId;
userName = msg.userName;
socket.join(roomId);
console.log(
`information for ${socket.id} id => room: ${roomId}; username: ${userName} `
);
// Add code for RSA key pair gen and storage
const { publicKey, privateKey } = crypto.generateKeyPairSync("rsa", {
modulusLength: 4096,
publicKeyEncoding: {
type: "spki",
format: "pem",
},
privateKeyEncoding: {
type: "pkcs8",
format: "pem",
},
});
console.log(`added public key for ${userName}`);
redisClient.set(userName, publicKey);
socket.emit("private-key-emit", privateKey);
});
socket.on("chat-message", (roomId, msg) => {
console.log(`message recieved - ${JSON.stringify(msg)}`);
socket.to(roomId).broadcast.emit(
"chat-message-broadcast",
JSON.stringify({
message: msg.message,
id: socket.id,
})
);
});
socket.on("public-key-request", (roomId, userName) => {
console.log("sending public key");
var publicKey;
redisClient.get(userName, function (err, reply) {
console.log(`obtained public key for ${userName}`);
socket.emit("public-key-response", reply);
});
});
socket.on("disconnect", () =>
console.log("user disconnected from room.")
);
});
},
};