From e5f3086cfdaf0fd3fcae8a9d91db1f9276828875 Mon Sep 17 00:00:00 2001 From: liuxiaobo <1224730913@qq.com> Date: Thu, 29 May 2025 16:58:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- processor/processor.go | 1 + processor/processor_test.go | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/processor/processor.go b/processor/processor.go index f169cda..7861edd 100644 --- a/processor/processor.go +++ b/processor/processor.go @@ -37,6 +37,7 @@ func (h *Processor) RegisterMessage(cmd int32, msg, delegate any) { func (h *Processor) UnregisterMessage(cmd int32) { delete(h.types, cmd) + delete(h.delegates, cmd) } func (h *Processor) GetMessageType(cmd int32) reflect.Type { diff --git a/processor/processor_test.go b/processor/processor_test.go index 00e510e..d70946e 100644 --- a/processor/processor_test.go +++ b/processor/processor_test.go @@ -9,7 +9,7 @@ import ( func onChat(userId int64, req *pb.InternalMsg) { _ = userId - fmt.Println("onChat", string(req.Msg)) + fmt.Println("onChat.", string(req.Msg)) } func TestProcessor(t *testing.T) { @@ -26,3 +26,23 @@ func TestProcessor(t *testing.T) { t.Error(err) } } + +func registerMessage(p *Processor, cmd int32, msg any, cb any) { + p.RegisterMessage(cmd, msg, cb) +} + +func TestProcessorAny(t *testing.T) { + p := NewProcessor() + p.RegisterMessages(RegisterMetas{ + pb.MsgId_Internal: {pb.InternalMsg{}, onChat}, + }) + registerMessage(p, int32(pb.MsgId_Internal), pb.InternalMsg{}, onChat) + + tmp := &pb.InternalMsg{UserId: 1, ConnId: 1, MsgId: int32(pb.MsgId_Internal), Msg: []byte("hello world")} + data, _ := proto.Marshal(tmp) + + req, _ := p.Unmarshal(int32(pb.MsgId_Internal), data) + if err := p.Dispatch(int32(pb.MsgId_Internal), int64(1), req); err != nil { + t.Error(err) + } +}