添加消息分发器

This commit is contained in:
liuxiaobo 2025-05-27 00:23:46 +08:00
parent c7d29893fe
commit 92d0dcef62
2 changed files with 7 additions and 6 deletions

View File

@ -4,7 +4,7 @@
// protoc v6.31.0 // protoc v6.31.0
// source: internal.proto // source: internal.proto
package processor package pb
import ( import (
protoreflect "google.golang.org/protobuf/reflect/protoreflect" protoreflect "google.golang.org/protobuf/reflect/protoreflect"

View File

@ -2,11 +2,12 @@ package processor
import ( import (
"fmt" "fmt"
"github.com/fox/fox/processor/pb"
"github.com/golang/protobuf/proto" "github.com/golang/protobuf/proto"
"testing" "testing"
) )
func onChat(userId int64, req *InternalMsg) { func onChat(userId int64, req *pb.InternalMsg) {
_ = userId _ = userId
fmt.Println("onChat", string(req.Msg)) fmt.Println("onChat", string(req.Msg))
} }
@ -14,14 +15,14 @@ func onChat(userId int64, req *InternalMsg) {
func TestProcessor(t *testing.T) { func TestProcessor(t *testing.T) {
p := NewProcessor() p := NewProcessor()
p.RegisterMessages(RegisterMetas{ p.RegisterMessages(RegisterMetas{
MsgId_Internal: {InternalMsg{}, onChat}, pb.MsgId_Internal: {pb.InternalMsg{}, onChat},
}) })
tmp := &InternalMsg{UserId: 1, ConnId: 1, MsgId: int32(MsgId_Internal), Msg: []byte("hello world")} tmp := &pb.InternalMsg{UserId: 1, ConnId: 1, MsgId: int32(pb.MsgId_Internal), Msg: []byte("hello world")}
data, _ := proto.Marshal(tmp) data, _ := proto.Marshal(tmp)
req, _ := p.Unmarshal(int32(MsgId_Internal), data) req, _ := p.Unmarshal(int32(pb.MsgId_Internal), data)
if err := p.Dispatch(int32(MsgId_Internal), int64(1), req); err != nil { if err := p.Dispatch(int32(pb.MsgId_Internal), int64(1), req); err != nil {
t.Error(err) t.Error(err)
} }
} }