forked from devigned/signalr-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
91 lines (76 loc) · 2.42 KB
/
handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package signalr
import (
"context"
"encoding/json"
"fmt"
"reflect"
)
type (
// Handler is the default handler implementation for receiving SignalR invocations
Handler interface {
Default(ctx context.Context, target string, args []json.RawMessage) error
}
// HandlerFunc is a type converter that allows a func to be used as a `Handler`
HandlerFunc func(ctx context.Context, target string, args []json.RawMessage) error
// NotifiedHandler is a message handler which also provides an OnStart hook
NotifiedHandler interface {
Handler
OnStart()
}
defaultNotifiedHandler struct {
Handler
onStart func()
}
)
// Default redirects this call to the func that was provided
func (hf HandlerFunc) Default(ctx context.Context, target string, args []json.RawMessage) error {
return hf(ctx, target, args)
}
// NewNotifiedHandler creates a new `NotifiedHandler` for responding to SignalR invocations
func NewNotifiedHandler(base Handler, onStart func()) NotifiedHandler {
return &defaultNotifiedHandler{
Handler: base,
onStart: onStart,
}
}
// OnStart calls the onStart function provided in the `NewNotifiedHandler` func when the client is done negotiating the
// initial handshake with the SignalR service
func (nh defaultNotifiedHandler) OnStart() {
nh.onStart()
}
func dispatch(ctx context.Context, handler Handler, msg *InvocationMessage) error {
t := reflect.TypeOf(handler)
if method, ok := t.MethodByName(msg.Target); ok {
mt := method.Type
numIn := mt.NumIn()
if numIn == len(msg.Arguments)+2 && method.Name != "Default" { // account for instance + context + arguments
args := make([]reflect.Value, mt.NumIn()-1)
args[0] = reflect.ValueOf(ctx)
for i := 0; i < len(msg.Arguments); i++ {
argType := mt.In(i + 2)
newArg := reflect.New(argType)
newArgPtr := newArg.Interface()
err := json.Unmarshal(msg.Arguments[i], &newArgPtr)
if err != nil {
fmt.Println(err)
return handler.Default(ctx, msg.Target, msg.Arguments)
}
args[i+1] = newArg.Elem()
}
actualTarget := reflect.ValueOf(handler).MethodByName(msg.Target)
returns := actualTarget.Call(args)
if len(returns) == 0 {
return nil
}
if returns[0].IsNil() {
return nil
}
if val, ok := returns[0].Elem().Interface().(error); ok {
return val
}
fmt.Printf("ran into a return we didn't know how to deal with: %+v", returns)
return nil
}
}
return handler.Default(ctx, msg.Target, msg.Arguments)
}