Wechaty机器人WatchdogAgent重置报错求助:长时间无消息触发
解决Wechaty WatchdogAgent重置报错的方案
原因说明
报错核心是WatchdogAgent监测到browserbridge心跳超时(默认60秒),触发重置机制,且未捕获error事件导致进程直接崩溃,通常出现在机器人长时间无消息交互、连接闲置的场景。
具体解决方法
1. 捕获并处理Wechaty的Error事件
未处理的error事件是导致进程崩溃的直接原因,必须先添加监听避免崩溃:
const { Wechaty } = require('wechaty'); const bot = Wechaty.instance({ // 你的机器人配置 }); // 关键:添加error事件监听 bot.on('error', (error) => { console.error('机器人捕获到错误:', error); // 可选:在这里实现自动重连逻辑 bot.stop().then(() => bot.start()); }); bot.start();
2. 调整Watchdog超时阈值
通过Puppet配置延长心跳超时时间,避免因短时间闲置触发重置:
const { Wechaty } = require('wechaty'); const { PuppetWeChat } = require('wechaty-puppet-wechat'); const puppet = new PuppetWeChat({ watchdog: { timeoutMilliseconds: 300000, // 改为5分钟(可根据实际需求调整) }, }); const bot = Wechaty.instance({ puppet }); bot.start();
3. 主动发送心跳维持连接
定时触发心跳事件,给Watchdog喂"食物",避免超时:
const { Wechaty } = require('wechaty'); const bot = Wechaty.instance({ // 你的配置 }); bot.on('login', () => { // 每4分10秒发送一次心跳(需小于设置的超时时间) setInterval(async () => { if (bot.puppet) { // 触发browserbridge心跳 await bot.puppet.ding(); } }, 250000); }); bot.start();
4. 升级Wechaty及相关依赖
部分旧版本存在Watchdog逻辑bug,升级到最新稳定版可修复已知问题:
npm update wechaty wechaty-puppet wechaty-puppet-wechat
内容的提问来源于stack exchange,提问作者Adxm
相关产品推荐
相关产品推荐

