fix(ipc): Propagate per-client disconnects, add proper routing for targeted messages, and remove unused node-ipc deps
This commit is contained in:
@@ -211,6 +211,17 @@ export class IpcServer extends plugins.EventEmitter {
|
||||
this.isRunning = true;
|
||||
this.startClientIdleCheck();
|
||||
this.emit('start');
|
||||
|
||||
// Track individual client disconnects forwarded by the channel/transport
|
||||
this.primaryChannel.on('clientDisconnected', (clientId?: string) => {
|
||||
if (!clientId) return;
|
||||
// Clean up any topic subscriptions and client map entry
|
||||
this.cleanupClientSubscriptions(clientId);
|
||||
if (this.clients.has(clientId)) {
|
||||
this.clients.delete(clientId);
|
||||
this.emit('clientDisconnect', clientId);
|
||||
}
|
||||
});
|
||||
|
||||
// Handle readiness based on options
|
||||
if (options.readyWhen === 'accepting') {
|
||||
@@ -375,7 +386,14 @@ export class IpcServer extends plugins.EventEmitter {
|
||||
throw new Error(`Client ${clientId} not found`);
|
||||
}
|
||||
|
||||
await client.channel.sendMessage(type, payload, headers);
|
||||
// Ensure the target clientId is part of the headers so the transport
|
||||
// can route the message to the correct socket instead of broadcasting.
|
||||
const routedHeaders: Record<string, any> | undefined = {
|
||||
...(headers || {}),
|
||||
clientId,
|
||||
};
|
||||
|
||||
await client.channel.sendMessage(type, payload, routedHeaders);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -400,13 +418,12 @@ export class IpcServer extends plugins.EventEmitter {
|
||||
*/
|
||||
public async broadcast(type: string, payload: any, headers?: Record<string, any>): Promise<void> {
|
||||
const promises: Promise<void>[] = [];
|
||||
|
||||
for (const [clientId, client] of this.clients) {
|
||||
|
||||
for (const [clientId] of this.clients) {
|
||||
promises.push(
|
||||
client.channel.sendMessage(type, payload, headers)
|
||||
.catch((error) => {
|
||||
this.emit('error', error, clientId);
|
||||
})
|
||||
this.sendToClient(clientId, type, payload, headers).catch((error) => {
|
||||
this.emit('error', error, clientId);
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
@@ -423,14 +440,13 @@ export class IpcServer extends plugins.EventEmitter {
|
||||
headers?: Record<string, any>
|
||||
): Promise<void> {
|
||||
const promises: Promise<void>[] = [];
|
||||
|
||||
|
||||
for (const [clientId, client] of this.clients) {
|
||||
if (filter(clientId, client.metadata)) {
|
||||
promises.push(
|
||||
client.channel.sendMessage(type, payload, headers)
|
||||
.catch((error) => {
|
||||
this.emit('error', error, clientId);
|
||||
})
|
||||
this.sendToClient(clientId, type, payload, headers).catch((error) => {
|
||||
this.emit('error', error, clientId);
|
||||
})
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -552,4 +568,4 @@ export class IpcServer extends plugins.EventEmitter {
|
||||
public getIsReady(): boolean {
|
||||
return this.isReady;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user