mirror of
https://github.com/LLOneBot/LLOneBot.git
synced 2024-11-22 01:56:33 +00:00
add get_event api
This commit is contained in:
parent
f9454039a1
commit
7a33a36f44
@ -50,6 +50,7 @@ import { ForwardFriendSingleMsg, ForwardGroupSingleMsg } from './msg/ForwardSing
|
||||
import { GetGroupEssence } from './group/GetGroupEssence'
|
||||
import { GetGroupHonorInfo } from './group/GetGroupHonorInfo'
|
||||
import { GoCQHTTHandleQuickOperation } from './go-cqhttp/QuickOperation'
|
||||
import GetEvent from './llonebot/GetEvent'
|
||||
|
||||
export const actionHandlers = [
|
||||
new GetFile(),
|
||||
@ -59,6 +60,7 @@ export const actionHandlers = [
|
||||
new GetGroupAddRequest(),
|
||||
new SetQQAvatar(),
|
||||
new GetFriendWithCategory(),
|
||||
new GetEvent(),
|
||||
// onebot11
|
||||
new SendLike(),
|
||||
new GetMsg(),
|
||||
|
23
src/onebot11/action/llonebot/GetEvent.ts
Normal file
23
src/onebot11/action/llonebot/GetEvent.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import BaseAction from '../BaseAction'
|
||||
import { ActionName } from '../types'
|
||||
import { getHttpEvent } from '../../server/event-for-http'
|
||||
import { PostEventType } from '../../server/post-ob11-event'
|
||||
// import { log } from "../../../common/utils";
|
||||
|
||||
interface Payload {
|
||||
key: string
|
||||
timeout: number
|
||||
}
|
||||
|
||||
export default class GetEvent extends BaseAction<Payload, PostEventType[]> {
|
||||
actionName = ActionName.GetEvent
|
||||
protected async _handle(payload: Payload): Promise<PostEventType[]> {
|
||||
let key = ''
|
||||
if (payload.key) {
|
||||
key = payload.key;
|
||||
}
|
||||
let timeout = parseInt(payload.timeout?.toString()) || 0;
|
||||
let evts = await getHttpEvent(key,timeout);
|
||||
return evts;
|
||||
}
|
||||
}
|
@ -22,6 +22,7 @@ export enum ActionName {
|
||||
Debug = 'llonebot_debug',
|
||||
GetFile = 'get_file',
|
||||
GetFriendsWithCategory = 'get_friends_with_category',
|
||||
GetEvent = 'get_event',
|
||||
// onebot 11
|
||||
SendLike = 'send_like',
|
||||
GetLoginInfo = 'get_login_info',
|
||||
|
68
src/onebot11/server/event-for-http.ts
Normal file
68
src/onebot11/server/event-for-http.ts
Normal file
@ -0,0 +1,68 @@
|
||||
import { PostEventType } from "./post-ob11-event"
|
||||
|
||||
|
||||
interface HttpEventType {
|
||||
seq: number
|
||||
event: PostEventType
|
||||
}
|
||||
|
||||
interface HttpUserType {
|
||||
lastAccessTime: number
|
||||
userSeq: number
|
||||
}
|
||||
|
||||
let curentSeq:number = 0;
|
||||
let eventList:HttpEventType[] = [];
|
||||
let httpUser:Record<string,HttpUserType> = {};
|
||||
|
||||
|
||||
export function postHttpEvent(event: PostEventType) {
|
||||
curentSeq += 1;
|
||||
eventList.push({
|
||||
seq: curentSeq,
|
||||
event: event
|
||||
});
|
||||
while(eventList.length > 100) {
|
||||
eventList.shift();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export async function getHttpEvent(userKey:string,timeout = 0) {
|
||||
let toRetEvent = [];
|
||||
|
||||
// 清除过时的user,5分钟没访问过的user将被删除
|
||||
let now = Date.now();
|
||||
for(let key in httpUser) {
|
||||
let user = httpUser[key];
|
||||
if(now - user.lastAccessTime > 1000 * 60 * 5) {
|
||||
delete httpUser[key];
|
||||
}
|
||||
}
|
||||
|
||||
// 增加新的user
|
||||
if(!httpUser[userKey] ) {
|
||||
httpUser[userKey] = {
|
||||
lastAccessTime: now,
|
||||
userSeq: curentSeq
|
||||
}
|
||||
}
|
||||
|
||||
let user = httpUser[userKey];
|
||||
// 等待数据到来,暂时先这么写吧......
|
||||
while(curentSeq == user.userSeq && Date.now() - now < timeout) {
|
||||
await new Promise( resolve => setTimeout(resolve, 10) );
|
||||
}
|
||||
// 取数据
|
||||
for(let i = 0; i < eventList.length; i++) {
|
||||
let evt = eventList[i];
|
||||
if(evt.seq > user.userSeq) {
|
||||
toRetEvent.push(evt.event);
|
||||
}
|
||||
}
|
||||
|
||||
// 更新user数据
|
||||
user.lastAccessTime = Date.now();
|
||||
user.userSeq = curentSeq;
|
||||
return toRetEvent;
|
||||
}
|
@ -8,6 +8,7 @@ import { log } from '@/common/utils'
|
||||
import { getConfigUtil } from '@/common/config'
|
||||
import crypto from 'crypto'
|
||||
import { handleQuickOperation, QuickOperationEvent } from '../action/quick-operation'
|
||||
import { postHttpEvent } from './event-for-http'
|
||||
|
||||
export type PostEventType = OB11Message | OB11BaseMetaEvent | OB11BaseNoticeEvent
|
||||
|
||||
@ -78,4 +79,9 @@ export function postOb11Event(msg: PostEventType, reportSelf = false, postWs = t
|
||||
if (postWs) {
|
||||
postWsEvent(msg)
|
||||
}
|
||||
if(!(msg.post_type == 'meta_event' && (msg as OB11BaseMetaEvent).meta_event_type == 'heartbeat')) {
|
||||
// 不上报心跳
|
||||
postHttpEvent(msg)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user