⬇️ Fixing ADC++ socket download notifications
This commit is contained in:
@@ -9,6 +9,11 @@ const { MoleculerError } = require("moleculer").Errors;
|
|||||||
const SocketIOService = require("moleculer-io");
|
const SocketIOService = require("moleculer-io");
|
||||||
const { v4: uuidv4 } = require("uuid");
|
const { v4: uuidv4 } = require("uuid");
|
||||||
import AirDCPPSocket from "../shared/airdcpp.socket";
|
import AirDCPPSocket from "../shared/airdcpp.socket";
|
||||||
|
import type { Socket as IOSocket } from "socket.io";
|
||||||
|
import { namespace } from "../moleculer.config";
|
||||||
|
|
||||||
|
// Context type carrying the Socket.IO socket in meta
|
||||||
|
type SocketCtx<P> = Context<P, { socket: IOSocket }>;
|
||||||
|
|
||||||
export default class SocketService extends Service {
|
export default class SocketService extends Service {
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
@@ -130,7 +135,6 @@ export default class SocketService extends Service {
|
|||||||
},
|
},
|
||||||
async handler(ctx) {
|
async handler(ctx) {
|
||||||
const { query, config, namespace } = ctx.params;
|
const { query, config, namespace } = ctx.params;
|
||||||
console.log("NAMESPACE", namespace)
|
|
||||||
const namespacedInstance = this.io.of(namespace || "/");
|
const namespacedInstance = this.io.of(namespace || "/");
|
||||||
const ADCPPSocket = new AirDCPPSocket(config);
|
const ADCPPSocket = new AirDCPPSocket(config);
|
||||||
try {
|
try {
|
||||||
@@ -297,34 +301,28 @@ export default class SocketService extends Service {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
listenBundleTick: {
|
listenFileProgress: {
|
||||||
async handler(ctx) {
|
params: { config: "object", namespace: "string" },
|
||||||
const { config } = ctx.params;
|
async handler(
|
||||||
|
ctx: SocketCtx<{ config: any; namespace: string }>
|
||||||
|
) {
|
||||||
|
const { config, namespace } = ctx.params;
|
||||||
|
const namespacedInstance = this.io.of(namespace || "/");
|
||||||
const ADCPPSocket = new AirDCPPSocket(config);
|
const ADCPPSocket = new AirDCPPSocket(config);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Connect once
|
||||||
await ADCPPSocket.connect();
|
await ADCPPSocket.connect();
|
||||||
console.log("Connected to AirDCPP successfully.");
|
await ADCPPSocket.addListener(
|
||||||
|
|
||||||
ADCPPSocket.addListener(
|
|
||||||
"queue",
|
"queue",
|
||||||
"queue_bundle_tick",
|
"queue_bundle_tick",
|
||||||
(tickData) => {
|
async (data) => {
|
||||||
console.log(
|
console.log(
|
||||||
"Received tick data: ",
|
`is mulk ne har shakz ko jo kaam tha saupa \nus shakz ne us kaam ki maachis jala di`
|
||||||
tickData
|
|
||||||
);
|
);
|
||||||
this.io.emit("bundleTickUpdate", tickData);
|
namespacedInstance.emit("downloadTick", data)
|
||||||
},
|
}
|
||||||
null
|
|
||||||
); // Assuming no specific ID is needed here
|
|
||||||
} catch (error) {
|
|
||||||
console.error(
|
|
||||||
"Error connecting to AirDCPP or setting listener:",
|
|
||||||
error
|
|
||||||
);
|
);
|
||||||
throw error;
|
} catch {}
|
||||||
}
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
@@ -333,15 +331,19 @@ export default class SocketService extends Service {
|
|||||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
},
|
},
|
||||||
handleSocketConnection: async function (socket: any) {
|
handleSocketConnection: async function (socket: any) {
|
||||||
this.logger.info(`Socket connected with session ID: ${socket.id}`);
|
this.logger.info(
|
||||||
|
`Socket connected with session ID: ${socket.id}`
|
||||||
|
);
|
||||||
console.log("Looking up sessionId in Mongo...");
|
console.log("Looking up sessionId in Mongo...");
|
||||||
|
|
||||||
const sessionIdExists = await Session.find({
|
const sessionIdExists = await Session.find({
|
||||||
sessionId: socket.handshake.query.sessionId,
|
sessionId: socket.handshake.query.sessionId,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (sessionIdExists.length === 0) {
|
if (sessionIdExists.length === 0) {
|
||||||
console.log(`Socket Id ${socket.id} not found in Mongo, creating a new session...`);
|
console.log(
|
||||||
|
`Socket Id ${socket.id} not found in Mongo, creating a new session...`
|
||||||
|
);
|
||||||
const sessionId = uuidv4();
|
const sessionId = uuidv4();
|
||||||
socket.sessionId = sessionId;
|
socket.sessionId = sessionId;
|
||||||
console.log(`Saving session ${sessionId} to Mongo...`);
|
console.log(`Saving session ${sessionId} to Mongo...`);
|
||||||
@@ -353,12 +355,14 @@ export default class SocketService extends Service {
|
|||||||
} else {
|
} else {
|
||||||
console.log(`Found socketId ${socket.id}, no-op.`);
|
console.log(`Found socketId ${socket.id}, no-op.`);
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
async started() {
|
async started() {
|
||||||
this.io.of("/manual").on("connection", async (socket) => {
|
this.io.of("/manual").on("connection", async (socket) => {
|
||||||
console.log(`socket.io server connected to /manual namespace`);
|
console.log(
|
||||||
})
|
`socket.io server connected to /manual namespace`
|
||||||
|
);
|
||||||
|
});
|
||||||
this.io.on("connection", async (socket) => {
|
this.io.on("connection", async (socket) => {
|
||||||
console.log(
|
console.log(
|
||||||
`socket.io server connected to client with session ID: ${socket.id}`
|
`socket.io server connected to client with session ID: ${socket.id}`
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ class AirDCPPSocket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Method to add listeners to the socket instance for handling real-time updates or events
|
// Method to add listeners to the socket instance for handling real-time updates or events
|
||||||
async addListener(event: any, handlerName: any, callback: any, id: any) {
|
async addListener(event: any, handlerName: any, callback: any, id?: any) {
|
||||||
// Attach a listener to the socket instance
|
// Attach a listener to the socket instance
|
||||||
return await this.socketInstance.addListener(
|
return await this.socketInstance.addListener(
|
||||||
event,
|
event,
|
||||||
|
|||||||
Reference in New Issue
Block a user