From b3a7461548da84c3a0505d2e5fc0c278e15cd258 Mon Sep 17 00:00:00 2001 From: zyh Date: Sun, 29 Mar 2026 23:15:18 +0000 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=94=B9=E7=94=A8=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6=E6=9B=BF=E4=BB=A3=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E8=AF=BB=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 config.json 存储环信配置和测试账号 - 修改 test-easemob-msg.js 从配置文件读取配置 - 移除 sql.js 依赖,简化项目依赖 - 更新测试脚本注释说明 Co-Authored-By: Claude Co-Authored-By: Happy --- config.json | 25 +++++++++++ package.json | 1 - scripts/test-easemob-msg.js | 90 +++++++++++-------------------------- 3 files changed, 50 insertions(+), 66 deletions(-) create mode 100644 config.json diff --git a/config.json b/config.json new file mode 100644 index 0000000..3f48a93 --- /dev/null +++ b/config.json @@ -0,0 +1,25 @@ +{ + "easemob": { + "enabled": true, + "appKey": "1101260329209923#demo", + "restServer": "https://a1.easemob.com", + "socketServer": "wss://im-api-v2.easemob.com/ws", + "debug": true + }, + "accounts": { + "bot": { + "label": "originclaw_bot", + "user": "originclaw_bot", + "pwd": "OriginClaw2026!" + }, + "tester": { + "label": "testuser1", + "user": "testuser1", + "pwd": "Test123456" + } + }, + "restApi": { + "clientId": "YXA6kkv90BPkRMKePRDWROyxzA", + "clientSecret": "YXA6aMhXlWR6gefM4gWTQ68TyVy9klY" + } +} diff --git a/package.json b/package.json index 5f00553..8df4547 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,6 @@ }, "dependencies": { "easemob-websdk": "^4.19.1", - "sql.js": "^1.13.0", "ws": "^8.18.0", "xhr2": "^0.2.1" }, diff --git a/scripts/test-easemob-msg.js b/scripts/test-easemob-msg.js index b9c6e12..ac35882 100644 --- a/scripts/test-easemob-msg.js +++ b/scripts/test-easemob-msg.js @@ -1,13 +1,8 @@ /** * 环信 SDK 双用户消息互发测试脚本 * - * 同时登录两个环信账号(originclaw_bot 和 testuser1), - * 测试双向消息收发: - * 1. 双方各自连接 + 登录 - * 2. testuser1 -> originclaw_bot 发送消息 - * 3. originclaw_bot 收到后自动回复 - * 4. originclaw_bot -> testuser1 主动发消息 - * 5. testuser1 收到后自动回复 + * 同时登录两个环信账号,测试双向消息收发。 + * 配置从项目根目录的 config.json 文件读取。 * * 用法: * node scripts/test-easemob-msg.js # 默认互发测试 @@ -18,49 +13,23 @@ const path = require('path'); const fs = require('fs'); // ────────────────────────────────────────── -// 测试账号配置 +// 从配置文件读取配置 // ────────────────────────────────────────── -const ACCOUNTS = { - bot: { - label: 'originclaw_bot', - user: 'originclaw_bot', - pwd: 'OriginClaw2026!', - }, - tester: { - label: 'testuser1', - user: 'testuser1', - pwd: 'Test123456', - }, -}; -const APP_KEY = '1101260329209923#demo'; +function loadConfig() { + const configPath = path.join(__dirname, '..', 'config.json'); -// ────────────────────────────────────────── -// Step 0: 从 SQLite 读取配置(获取服务器地址等) -// ────────────────────────────────────────── -async function loadConfigFromDB() { - const initSqlJs = require('sql.js'); - const dbPath = path.join(process.env.APPDATA, 'OriginClawAI', 'originclawai.sqlite'); - - if (!fs.existsSync(dbPath)) { - console.warn(`⚠ 数据库文件不存在: ${dbPath},使用默认配置`); - return null; + if (!fs.existsSync(configPath)) { + throw new Error(`配置文件不存在: ${configPath}`); } - const SQL = await initSqlJs(); - const buf = fs.readFileSync(dbPath); - const db = new SQL.Database(buf); - - const result = db.exec("SELECT value FROM im_config WHERE key = 'easemob'"); - db.close(); - - if (!result[0]?.values?.[0]?.[0]) { - console.warn('⚠ 数据库中没有环信配置,使用默认配置'); - return null; - } - - return JSON.parse(result[0].values[0][0]); + const content = fs.readFileSync(configPath, 'utf-8'); + return JSON.parse(content); } +const config = loadConfig(); +const ACCOUNTS = config.accounts; +const APP_KEY = config.easemob.appKey; + // ────────────────────────────────────────── // Polyfill // ────────────────────────────────────────── @@ -69,10 +38,10 @@ const { applyManualPolyfill } = require('./easemob-polyfill'); // ────────────────────────────────────────── // 为账号创建连接实例并登录 // ────────────────────────────────────────── -function createConnection(account, websdk, config) { +function createConnection(account, websdk, appConfig) { const conn = new websdk.connection({ - appKey: APP_KEY, - debug: false, + appKey: appConfig.appKey, + debug: appConfig.debug || false, }); const peer = account.label === 'bot' ? ACCOUNTS.tester : ACCOUNTS.bot; @@ -156,20 +125,11 @@ async function main() { console.log(` 初始消息: "${customMsg}"`); console.log('================================\n'); - // Step 1: 读取数据库配置 - console.log('[Step 1] 读取配置...'); - let config; - try { - config = await loadConfigFromDB(); - if (config) { - console.log(` AppKey: ${config.appKey}`); - console.log(` REST Server: ${config.restServer || '(默认)'}`); - } else { - console.log(' 使用硬编码默认配置'); - } - } catch (err) { - console.warn(' ⚠ 读取配置失败,使用默认:', err.message); - } + // Step 1: 显示配置 + console.log('[Step 1] 加载配置...'); + console.log(` AppKey: ${config.easemob.appKey}`); + console.log(` REST Server: ${config.easemob.restServer}`); + console.log(` Socket Server: ${config.easemob.socketServer}`); // Step 2: Polyfill console.log('\n[Step 2] 应用 Polyfill...'); @@ -197,7 +157,7 @@ async function main() { try { const { XMLHttpRequest } = require('xhr2'); globalThis.XMLHttpRequest = XMLHttpRequest; - const baseUrl = config?.restServer || 'https://a1.easemob.com'; + const baseUrl = config.easemob.restServer; const origOpen = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function patchedOpen(method, url, ...rest) { let finalUrl = url; @@ -206,7 +166,7 @@ async function main() { } return origOpen.call(this, method, finalUrl, ...rest); }; - console.log(' + XMLHttpRequest = xhr2'); + console.log(` + XMLHttpRequest = xhr2 (base: ${baseUrl})`); } catch { console.warn(' ! xhr2 包未安装'); } @@ -226,8 +186,8 @@ async function main() { // Step 4: 创建两个连接 console.log('\n[Step 4] 创建双用户连接...'); - const botCtx = createConnection(ACCOUNTS.bot, websdk, config); - const testerCtx = createConnection(ACCOUNTS.tester, websdk, config); + const botCtx = createConnection(ACCOUNTS.bot, websdk, config.easemob); + const testerCtx = createConnection(ACCOUNTS.tester, websdk, config.easemob); console.log(' 两个连接实例创建成功 ✓');