1
0

tgbase.ts 936 B

1234567891011121314151617181920212223
  1. import BaseService from '../../base';
  2. export default class TgBaseService extends BaseService {
  3. async reqTgbotApi(cmd:string, data:any) {
  4. // 生成一个唯一的请求 ID
  5. const timeout = 1000; // 超时时间,单位:毫秒
  6. const reqId = Math.random().toString(36).slice(2);
  7. this.ctx.app.messenger.sendToApp('req-tgboot-api', { reqId, cmd, data });
  8. // 等待 worker 返回结果
  9. return await new Promise(resolve => {
  10. const timeoutId = setTimeout(() => {
  11. console.error('Request timeout: Tgboot did not respond in time');
  12. // 挂了 告诉代理重新选一个进程暂时 后边搞到后台
  13. this.ctx.app.messenger.sendToAgent('reset-tgboot-api', {});
  14. resolve(null);
  15. }, timeout);
  16. this.ctx.app.messenger.once(`resp-tgboot-api-${reqId}`, response => {
  17. clearTimeout(timeoutId); // 清除超时定时器
  18. resolve(response);
  19. });
  20. });
  21. }
  22. }