mirror of
https://github.com/NapNeko/NapCatQQ.git
synced 2025-07-19 12:03:37 +00:00
fix: 一些问题
This commit is contained in:
@@ -33,7 +33,7 @@ import { NodeIO3MiscListener } from '@/core/listeners/NodeIO3MiscListener';
|
|||||||
import { sleep } from '@/common/helper';
|
import { sleep } from '@/common/helper';
|
||||||
import { downloadFFmpegIfNotExists } from '@/common/download-ffmpeg';
|
import { downloadFFmpegIfNotExists } from '@/common/download-ffmpeg';
|
||||||
import { FFmpegService } from '@/common/ffmpeg';
|
import { FFmpegService } from '@/common/ffmpeg';
|
||||||
|
import { connectToNamedPipe } from '@/shell/pipe';
|
||||||
// NapCat Shell App ES 入口文件
|
// NapCat Shell App ES 入口文件
|
||||||
async function handleUncaughtExceptions(logger: LogWrapper) {
|
async function handleUncaughtExceptions(logger: LogWrapper) {
|
||||||
process.on('uncaughtException', (err) => {
|
process.on('uncaughtException', (err) => {
|
||||||
@@ -313,9 +313,10 @@ export async function NCoreInitShell() {
|
|||||||
const pathWrapper = new NapCatPathWrapper();
|
const pathWrapper = new NapCatPathWrapper();
|
||||||
const logger = new LogWrapper(pathWrapper.logsPath);
|
const logger = new LogWrapper(pathWrapper.logsPath);
|
||||||
handleUncaughtExceptions(logger);
|
handleUncaughtExceptions(logger);
|
||||||
|
await connectToNamedPipe(logger).catch(e => logger.logError('命名管道连接失败', e));
|
||||||
downloadFFmpegIfNotExists(logger).then(({ path, reset }) => {
|
downloadFFmpegIfNotExists(logger).then(({ path, reset }) => {
|
||||||
if (reset && path) {
|
if (reset && path) {
|
||||||
FFmpegService.setFfmpegPath(path,logger);
|
FFmpegService.setFfmpegPath(path, logger);
|
||||||
}
|
}
|
||||||
}).catch(e => {
|
}).catch(e => {
|
||||||
logger.logError('[Ffmpeg] Error:', e);
|
logger.logError('[Ffmpeg] Error:', e);
|
||||||
|
@@ -1,36 +1,2 @@
|
|||||||
import { NCoreInitShell } from './base';
|
import { NCoreInitShell } from './base';
|
||||||
import * as net from 'net';
|
|
||||||
import * as process from 'process';
|
|
||||||
if (process.platform === 'win32') {
|
|
||||||
const pid = process.pid;
|
|
||||||
const pipePath = `\\\\.\\pipe\\NapCat_${pid}`;
|
|
||||||
try {
|
|
||||||
const pipeSocket = net.connect(pipePath, () => {
|
|
||||||
console.log(`已连接到命名管道: ${pipePath}`);
|
|
||||||
process.stdout.write = (
|
|
||||||
chunk: any,
|
|
||||||
encoding?: BufferEncoding | (() => void),
|
|
||||||
cb?: () => void
|
|
||||||
): boolean => {
|
|
||||||
if (typeof encoding === 'function') {
|
|
||||||
cb = encoding;
|
|
||||||
encoding = undefined;
|
|
||||||
}
|
|
||||||
return pipeSocket.write(chunk, encoding as BufferEncoding, cb);
|
|
||||||
};
|
|
||||||
console.log(`stdout 已重定向到命名管道: ${pipePath}`);
|
|
||||||
});
|
|
||||||
|
|
||||||
pipeSocket.on('error', (err) => {
|
|
||||||
console.log(`连接命名管道 ${pipePath} 时出错:`, err);
|
|
||||||
});
|
|
||||||
|
|
||||||
pipeSocket.on('end', () => {
|
|
||||||
console.log('命名管道连接已关闭');
|
|
||||||
});
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.log(`尝试连接命名管道 ${pipePath} 时发生异常:`, error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
NCoreInitShell();
|
NCoreInitShell();
|
74
src/shell/pipe.ts
Normal file
74
src/shell/pipe.ts
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
import { LogWrapper } from '@/common/log';
|
||||||
|
import * as net from 'net';
|
||||||
|
import * as process from 'process';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 连接到命名管道并重定向stdout
|
||||||
|
* @param logger 日志记录器
|
||||||
|
* @param timeoutMs 连接超时时间(毫秒),默认5000ms
|
||||||
|
* @returns Promise,连接成功时resolve,失败时reject
|
||||||
|
*/
|
||||||
|
export function connectToNamedPipe(logger: LogWrapper, timeoutMs: number = 5000): Promise<{ disconnect: () => void }> {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (process.platform !== 'win32') {
|
||||||
|
logger.log('只有Windows平台支持命名管道');
|
||||||
|
// 非Windows平台不reject,而是返回一个空的disconnect函数
|
||||||
|
return resolve({ disconnect: () => { } });
|
||||||
|
}
|
||||||
|
|
||||||
|
const pid = process.pid;
|
||||||
|
const pipePath = `\\\\.\\pipe\\NapCat_${pid}`;
|
||||||
|
|
||||||
|
// 设置连接超时
|
||||||
|
const timeoutId = setTimeout(() => {
|
||||||
|
reject(new Error(`连接命名管道超时: ${pipePath}`));
|
||||||
|
}, timeoutMs);
|
||||||
|
|
||||||
|
try {
|
||||||
|
let originalStdoutWrite = process.stdout.write.bind(process.stdout);
|
||||||
|
const pipeSocket = net.connect(pipePath, () => {
|
||||||
|
// 清除超时
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
|
||||||
|
logger.log(`[StdOut] 已重定向到命名管道: ${pipePath}`);
|
||||||
|
process.stdout.write = (
|
||||||
|
chunk: any,
|
||||||
|
encoding?: BufferEncoding | (() => void),
|
||||||
|
cb?: () => void
|
||||||
|
): boolean => {
|
||||||
|
if (typeof encoding === 'function') {
|
||||||
|
cb = encoding;
|
||||||
|
encoding = undefined;
|
||||||
|
}
|
||||||
|
return pipeSocket.write(chunk, encoding as BufferEncoding, cb);
|
||||||
|
};
|
||||||
|
// 提供断开连接的方法
|
||||||
|
const disconnect = () => {
|
||||||
|
process.stdout.write = originalStdoutWrite;
|
||||||
|
pipeSocket.end();
|
||||||
|
logger.log(`已手动断开命名管道连接: ${pipePath}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
// 返回成功和断开连接的方法
|
||||||
|
resolve({ disconnect });
|
||||||
|
});
|
||||||
|
|
||||||
|
pipeSocket.on('error', (err) => {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
process.stdout.write = originalStdoutWrite;
|
||||||
|
logger.log(`连接命名管道 ${pipePath} 时出错:`, err);
|
||||||
|
reject(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
pipeSocket.on('end', () => {
|
||||||
|
process.stdout.write = originalStdoutWrite;
|
||||||
|
logger.log('命名管道连接已关闭');
|
||||||
|
});
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
clearTimeout(timeoutId);
|
||||||
|
logger.log(`尝试连接命名管道 ${pipePath} 时发生异常:`, error);
|
||||||
|
reject(error);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
Reference in New Issue
Block a user