123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- package service
- import (
- "fmt"
- "git.bvbej.com/bvbej/base-golang/pkg/websocket/codec"
- "git.bvbej.com/bvbej/base-golang/pkg/websocket/util"
- "reflect"
- "strings"
- )
- type Handler struct {
- Receiver reflect.Value // 值
- Method reflect.Method // 方法
- Type reflect.Type // 类型
- IsRawArg bool // 数据是否需要序列化
- }
- func RegisterHandler(components ...interface{}) map[string]*Handler {
- methods := make(map[string]*Handler)
- for _, component := range components {
- rt := reflect.TypeOf(component)
- rv := reflect.ValueOf(component)
- typeName := reflect.Indirect(rv).Type().Name()
- if typeName == "" {
- continue
- }
- if !util.IsExported(typeName) {
- continue
- }
- for m := 0; m < rt.NumMethod(); m++ {
- method := rt.Method(m)
- mt := method.Type
- mn := method.Name
- if isHandlerMethod(method) {
- raw := false
- if mt.In(2) == typeOfBytes {
- raw = true
- }
- router := fmt.Sprintf("%s.%s", strings.ToLower(typeName), strings.ToLower(mn))
- methods[router] = &Handler{
- Receiver: rv,
- Method: method,
- Type: mt.In(2),
- IsRawArg: raw,
- }
- }
- }
- }
- for router, handler := range methods {
- codec.RegisterMessage(router, handler.Type)
- }
- return methods
- }
|