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