-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathclient_socket_events.go
97 lines (82 loc) · 2.33 KB
/
client_socket_events.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
92
93
94
95
96
97
package sio
import (
"fmt"
"reflect"
)
func (s *clientSocket) OnEvent(eventName string, handler any) {
if IsEventReservedForClient(eventName) {
panic(fmt.Errorf("sio: OnEvent: attempted to register a reserved event: `%s`", eventName))
}
h, err := newEventHandler(handler)
if err != nil {
panic(err)
}
s.eventHandlers.on(eventName, h)
}
func (s *clientSocket) OnceEvent(eventName string, handler any) {
if IsEventReservedForClient(eventName) {
panic(fmt.Errorf("sio: OnceEvent: attempted to register a reserved event: `%s`", eventName))
}
h, err := newEventHandler(handler)
if err != nil {
panic(err)
}
s.eventHandlers.once(eventName, h)
}
func (s *clientSocket) OffEvent(eventName string, handler ...any) {
values := make([]reflect.Value, len(handler))
for i := range values {
values[i] = reflect.ValueOf(handler[i])
}
s.eventHandlers.off(eventName, values...)
}
func (s *clientSocket) OffAll() {
s.eventHandlers.offAll()
s.connectHandlers.offAll()
s.connectErrorHandlers.offAll()
s.disconnectHandlers.offAll()
}
type (
ClientSocketConnectFunc func()
ClientSocketConnectErrorFunc func(err any)
ClientSocketDisconnectFunc func(reason Reason)
)
func (s *clientSocket) OnConnect(f ClientSocketConnectFunc) {
s.connectHandlers.on(&f)
}
func (s *clientSocket) OnceConnect(f ClientSocketConnectFunc) {
s.connectHandlers.once(&f)
}
func (s *clientSocket) OffConnect(_f ...ClientSocketConnectFunc) {
f := make([]*ClientSocketConnectFunc, len(_f))
for i := range f {
f[i] = &_f[i]
}
s.connectHandlers.off(f...)
}
func (s *clientSocket) OnConnectError(f ClientSocketConnectErrorFunc) {
s.connectErrorHandlers.on(&f)
}
func (s *clientSocket) OnceConnectError(f ClientSocketConnectErrorFunc) {
s.connectErrorHandlers.once(&f)
}
func (s *clientSocket) OffConnectError(_f ...ClientSocketConnectErrorFunc) {
f := make([]*ClientSocketConnectErrorFunc, len(_f))
for i := range f {
f[i] = &_f[i]
}
s.connectErrorHandlers.off(f...)
}
func (s *clientSocket) OnDisconnect(f ClientSocketDisconnectFunc) {
s.disconnectHandlers.on(&f)
}
func (s *clientSocket) OnceDisconnect(f ClientSocketDisconnectFunc) {
s.disconnectHandlers.once(&f)
}
func (s *clientSocket) OffDisconnect(_f ...ClientSocketDisconnectFunc) {
f := make([]*ClientSocketDisconnectFunc, len(_f))
for i := range f {
f[i] = &_f[i]
}
s.disconnectHandlers.off(f...)
}