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 // 类型 Name string //类名 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 } mn = strings.ToLower(mn) methods[mn] = &Handler{ Receiver: rv, Method: method, Type: mt.In(2), Name: strings.ToLower(typeName), IsRawArg: raw, } } } } for name, handler := range methods { router := fmt.Sprintf("%s.%s", handler.Name, name) codec.RegisterMessage(router, handler.Type) } return methods }