- 添加checkCppServiceConnection函数用于检查C++服务是否可连接 - 修改保存逻辑:如果C++服务可连接,先发送文件内容到C++服务,只有在收到200状态码后才继续保存到服务器 - 如果C++服务不可连接,则直接使用API函数保存到服务器 - 完善输出信息,帮助用户了解当前处理流程 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
139 lines
4.8 KiB
JavaScript
139 lines
4.8 KiB
JavaScript
"use strict";
|
||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
||
if (k2 === undefined) k2 = k;
|
||
var desc = Object.getOwnPropertyDescriptor(m, k);
|
||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
||
desc = { enumerable: true, get: function() { return m[k]; } };
|
||
}
|
||
Object.defineProperty(o, k2, desc);
|
||
}) : (function(o, m, k, k2) {
|
||
if (k2 === undefined) k2 = k;
|
||
o[k2] = m[k];
|
||
}));
|
||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
||
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
||
}) : function(o, v) {
|
||
o["default"] = v;
|
||
});
|
||
var __importStar = (this && this.__importStar) || (function () {
|
||
var ownKeys = function(o) {
|
||
ownKeys = Object.getOwnPropertyNames || function (o) {
|
||
var ar = [];
|
||
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
||
return ar;
|
||
};
|
||
return ownKeys(o);
|
||
};
|
||
return function (mod) {
|
||
if (mod && mod.__esModule) return mod;
|
||
var result = {};
|
||
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
||
__setModuleDefault(result, mod);
|
||
return result;
|
||
};
|
||
})();
|
||
Object.defineProperty(exports, "__esModule", { value: true });
|
||
exports.checkCppServiceConnection = checkCppServiceConnection;
|
||
exports.sendFileToCppService = sendFileToCppService;
|
||
const http = __importStar(require("http"));
|
||
const net = __importStar(require("net"));
|
||
const vscode = __importStar(require("vscode"));
|
||
/**
|
||
* 检查本地C++服务是否可连接
|
||
* @returns Promise<boolean> 是否可连接
|
||
*/
|
||
async function checkCppServiceConnection() {
|
||
// 从配置中获取端口号,默认为26000
|
||
const config = vscode.workspace.getConfiguration('squirrel');
|
||
const localServicePort = config.get('localServicePort', 26000);
|
||
return new Promise((resolve) => {
|
||
const socket = new net.Socket();
|
||
let isResolved = false;
|
||
socket.setTimeout(3000); // 3秒超时
|
||
socket.connect(localServicePort, '127.0.0.1', () => {
|
||
// 连接成功,端口可达
|
||
socket.destroy();
|
||
resolve(true);
|
||
});
|
||
socket.on('error', (error) => {
|
||
// 连接失败
|
||
if (!isResolved) {
|
||
isResolved = true;
|
||
resolve(false);
|
||
}
|
||
});
|
||
socket.on('timeout', () => {
|
||
// 连接超时
|
||
socket.destroy();
|
||
if (!isResolved) {
|
||
isResolved = true;
|
||
resolve(false);
|
||
}
|
||
});
|
||
socket.on('close', () => {
|
||
if (!isResolved) {
|
||
isResolved = true;
|
||
resolve(false);
|
||
}
|
||
});
|
||
});
|
||
}
|
||
/**
|
||
* 构建JSON数据
|
||
* @param filePath 文件路径
|
||
* @param content 文件内容
|
||
* @returns JSON字符串
|
||
*/
|
||
function buildJsonData(filePath, content) {
|
||
const data = {};
|
||
data[filePath] = content;
|
||
return JSON.stringify(data);
|
||
}
|
||
/**
|
||
* 发送文件内容到本地C++服务
|
||
* @param filePath 文件路径
|
||
* @param content 文件内容
|
||
* @returns Promise<boolean> 是否发送成功
|
||
*/
|
||
async function sendFileToCppService(filePath, content) {
|
||
// 从配置中获取端口号,默认为26000
|
||
const config = vscode.workspace.getConfiguration('squirrel');
|
||
const localServicePort = config.get('localServicePort', 26000);
|
||
return new Promise((resolve) => {
|
||
const jsonData = buildJsonData(filePath, content);
|
||
const postData = jsonData;
|
||
const options = {
|
||
hostname: '127.0.0.1',
|
||
port: localServicePort,
|
||
path: '/send_data',
|
||
method: 'POST',
|
||
headers: {
|
||
'Content-Type': 'application/json',
|
||
'Content-Length': Buffer.byteLength(postData)
|
||
}
|
||
};
|
||
const req = http.request(options, (res) => {
|
||
let data = '';
|
||
res.on('data', (chunk) => {
|
||
data += chunk;
|
||
});
|
||
res.on('end', () => {
|
||
console.log(`[Squirrel] 已发送文件到C++服务: ${filePath}, 状态码: ${res.statusCode}`);
|
||
if (res.statusCode === 200) {
|
||
resolve(true);
|
||
}
|
||
else {
|
||
console.error(`[Squirrel] 发送失败,状态码: ${res.statusCode}, 响应: ${data}`);
|
||
resolve(false);
|
||
}
|
||
});
|
||
});
|
||
req.on('error', (error) => {
|
||
console.error(`[Squirrel] 发送文件到C++服务失败: ${error.message}`);
|
||
resolve(false);
|
||
});
|
||
req.write(postData);
|
||
req.end();
|
||
});
|
||
}
|
||
//# sourceMappingURL=localClient.js.map
|