-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwsinvokehandler.go
More file actions
77 lines (64 loc) · 2.01 KB
/
wsinvokehandler.go
File metadata and controls
77 lines (64 loc) · 2.01 KB
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
package websocket
import (
"fmt"
"github.com/kklab-com/gone-core/channel"
"github.com/kklab-com/gone-http/http"
"github.com/kklab-com/goth-kklogger"
kkpanic "github.com/kklab-com/goth-panic"
)
type InvokeHandler struct {
channel.DefaultHandler
DefaultHandlerTask
task HandlerTask
params map[string]any
}
func NewInvokeHandler(task HandlerTask, params map[string]any) *InvokeHandler {
if params == nil {
params = map[string]any{}
}
return &InvokeHandler{task: task, params: params}
}
func (h *InvokeHandler) Read(ctx channel.HandlerContext, obj any) {
if ch, ok := ctx.Channel().(*Channel); ok {
if msg, ok := obj.(Message); ok {
h._Call(ctx, ch.Request, ch.Response, h.task, msg, h.params)
return
}
}
ctx.FireRead(obj)
return
}
func (h *InvokeHandler) Active(ctx channel.HandlerContext) {
if ch, ok := ctx.Channel().(*Channel); ok {
h.task.WSConnected(ch, ch.Request, ch.Response, h.params)
}
ctx.FireActive()
}
func (h *InvokeHandler) Inactive(ctx channel.HandlerContext) {
if ch, ok := ctx.Channel().(*Channel); ok {
h.task.WSDisconnected(ch, ch.Request, ch.Response, h.params)
}
ctx.FireInactive()
}
func (h *InvokeHandler) _Call(ctx channel.HandlerContext, req *http.Request, resp *http.Response, task HandlerTask, msg Message, params map[string]any) {
kkpanic.Catch(func() {
switch msg.Type() {
case TextMessageType:
task.WSText(ctx, msg.(*DefaultMessage), params)
case BinaryMessageType:
task.WSBinary(ctx, msg.(*DefaultMessage), params)
case CloseMessageType:
task.WSClose(ctx, msg.(*CloseMessage), params)
case PingMessageType:
task.WSPing(ctx, msg.(*PingMessage), params)
case PongMessageType:
task.WSPong(ctx, msg.(*PongMessage), params)
}
}, func(r kkpanic.Caught) {
kklogger.ErrorJ("websocket:InvokeHandler._Call", fmt.Sprintf("error occurred, %s", r.Error()))
task.WSErrorCaught(ctx, req, resp, msg, r)
})
}
func (h *InvokeHandler) ErrorCaught(ctx channel.HandlerContext, err error) {
kklogger.ErrorJ("websocket:InvokeHandler.ErrorCaught", err.Error())
}