'How can I be sure that socket request reached to my server?
I have the following code and each time that I send request to API, I want to make sure that my socket request reached the server successfully but I don't want to handle this on server side and I want to do it right in this file. is there a way for doing that and show the connection status with API? I want to handle it in both /api route and sendPrint event
const express = require("express");
const Joi = require("joi");
const app = express();
const io = require("socket.io-client");
const socket = io("socket_server_url", {
reconnectionDelayMax: 10000,
});
app.use(express.json());
let userIsConnected = false;
app.get("/api", (req, res) => {
const { client_id, printer_id, url } = req.query;
console.log(req.query);
if (userIsConnected) {
res.send("Connection succeed ");
} else {
res.send("Failed to connect to the server!");
}
const captcha = [clinet_id, printer_id, url];
socket.emit("sendPrint", captcha);
});
app.get("/send", (req, res) => {
res.send("send");
});
socket.on("connect", () => {
userIsConnected = true;
});
socket.off("sendPrint");
socket.on("sendPrint", (roomInfo) => {
console.log("Send Print Successful");
});
socket.on("disconnect", function () {
userIsConnected = false;
});
const port = process.env.PORT || 7878;
app.listen(port, () => console.log(`Listening on port ${port}..`));
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
