-
Notifications
You must be signed in to change notification settings - Fork 0
/
parent.js
38 lines (36 loc) · 1.07 KB
/
parent.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
const axios = require("axios");
const io = require("socket.io-client");
(async () => {
try {
const parentId = "parent_1";
const guardId = "guard_1";
const response = await axios.post("http://localhost:5050/generate-token", {
userId: parentId,
role: "parent",
});
const token = response.data.access_token;
const socket = io("http://localhost:5050", {
auth: {
token,
},
});
socket.on("connect", () => {
socket.emit("register_parent", parentId);
const data = "Hello, Guard!";
socket.emit("picket_request_guard", { parentId, guardId, data });
socket.on("picket_request_parent", ({ parentId, guardId, response }) => {
console.log(
`Response from Guard (guardId: ${guardId}) for Parent (parentId: ${parentId}): ${response}`
);
});
});
socket.on("connect_error", (err) => {
console.error("Connection Error:", err.message);
});
} catch (error) {
console.error(
"Error generating token or connecting to WebSocket:",
error.message
);
}
})();