Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加连接事件,request添加上下文 #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions onebot_action.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package libonebot

import (
"context"
"fmt"
)

Expand All @@ -22,8 +23,9 @@ func (ob *OneBot) Handle(handler Handler) {
// CallAction 调用指定动作.
//
// 参数:
// action: 要调用的动作名称
// params: 动作参数, 若传入 nil 则实际动作参数为空 map
//
// action: 要调用的动作名称
// params: 动作参数, 若传入 nil 则实际动作参数为空 map
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

似乎 gofmt 对注释的参数也开始格式化了,如果想再水个 pr 的话可以开个 pr 格式化所有代码🤓

func (ob *OneBot) CallAction(action string, params map[string]interface{}) Response {
if params == nil {
params = make(map[string]interface{})
Expand Down Expand Up @@ -54,7 +56,22 @@ func (ob *OneBot) handleRequest(r *Request) (resp Response) {
}

ob.Logger.Debugf("动作请求 `%v` 开始处理", r.Action)
ob.actionHandler.HandleAction(w, r)
ctx, cancelFunc := context.WithCancel(context.Background())
r.Ctx = ctx
go func() {
defer cancelFunc()
defer func() {
err := recover()
if err != nil {
if _, ok := err.(error); ok {
w.WriteFailed(RetCodeInternalHandlerError, err.(error))
}
ob.Logger.Errorf("处理%v时出现异常:%#v", r.Action, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

原谅我强迫症,这里需要注意一下空格和符号跟其他地方一致

"处理 %v 时出现异常: %#v"

}
}()
ob.actionHandler.HandleAction(w, r)
}()
<-ctx.Done()
if resp.Status == statusOK {
ob.Logger.Infof("动作请求 `%v` 处理成功", r.Action)
} else if resp.Status == statusFailed {
Expand Down
11 changes: 10 additions & 1 deletion onebot_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ type marshaledEvent struct {
}

func (ob *OneBot) openEventListenChan() <-chan marshaledEvent {
ch := make(chan marshaledEvent) // TODO: channel size
ch := make(chan marshaledEvent, 1) // 设置缓冲区为1,因为需要先放入一个连接事件
connectMetaEvent := MakeConnectMetaEvent(ob.Impl, Version, OneBotVersion)
ob.Logger.Debugf("事件: %#v", connectMetaEvent)
ob.Logger.Infof("事件 `%v` 开始推送", connectMetaEvent.Name())
eventBytes, _ := json.Marshal(connectMetaEvent)
ch <- marshaledEvent{
name: connectMetaEvent.Name(),
bytes: eventBytes,
raw: &connectMetaEvent,
}
Comment on lines +44 to +53
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

connect 事件应该只在 WS 和反向 WS 的时候产生,可能最好在 comm_ws.gocomm_ws_reverse.go 创建 eventChan 之后,开始 loop 之前创建并推送

ob.eventListenChansLock.Lock()
ob.eventListenChans = append(ob.eventListenChans, ch)
ob.eventListenChansLock.Unlock()
Expand Down
12 changes: 7 additions & 5 deletions proto_action_request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package libonebot

import (
"context"
"errors"

"github.com/botuniverse/go-libonebot/utils"
Expand All @@ -28,11 +29,12 @@ type RequestComm struct {

// Request 表示一个动作请求.
type Request struct {
Comm RequestComm // 接收动作请求的通信方式
Action string // 动作名称
Params EasierMap // 动作参数
Echo string // 动作请求的 echo 字段, 用户未指定时为空字符串
Self *Self // 机器人自身标识, 用户未指定时为 nil
Comm RequestComm // 接收动作请求的通信方式
Action string // 动作名称
Params EasierMap // 动作参数
Echo string // 动作请求的 echo 字段, 用户未指定时为空字符串
Self *Self // 机器人自身标识, 用户未指定时为 nil
Ctx context.Context // 上下文控制
}

func parseRequestFromMap(m map[string]interface{}, reqComm RequestComm) (r Request, err error) {
Expand Down
15 changes: 15 additions & 0 deletions proto_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ func MakeMetaEvent(time time.Time, detailType string) MetaEvent {
}
}

type VersionStruct struct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

直接叫 Version 就行

Impl string `json:"impl"`
Version string `json:"version"`
OnebotVersion string `json:"onebot_version"`
}

type ConnectMetaEvent struct {
MetaEvent
Version VersionStruct `json:"version"`
}
Comment on lines +95 to +104
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以加一下注释(根据标准文档里的备注)


func MakeConnectMetaEvent(impl, v, onebotVersion string) ConnectMetaEvent {
return ConnectMetaEvent{MetaEvent: MakeMetaEvent(time.Now(), "connect"), Version: VersionStruct{impl, v, onebotVersion}}
}

// MessageEvent 表示一个消息事件.
type MessageEvent struct {
Event
Expand Down