refactor: 改用配置文件替代数据库读取
- 新增 config.json 存储环信配置和测试账号 - 修改 test-easemob-msg.js 从配置文件读取配置 - 移除 sql.js 依赖,简化项目依赖 - 更新测试脚本注释说明 Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
25
config.json
Normal file
25
config.json
Normal file
@@ -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"
|
||||
}
|
||||
}
|
||||
@@ -9,7 +9,6 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"easemob-websdk": "^4.19.1",
|
||||
"sql.js": "^1.13.0",
|
||||
"ws": "^8.18.0",
|
||||
"xhr2": "^0.2.1"
|
||||
},
|
||||
|
||||
@@ -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(' 两个连接实例创建成功 ✓');
|
||||
|
||||
|
||||
Reference in New Issue
Block a user