-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.v1.runback.go
186 lines (169 loc) · 5.87 KB
/
server.v1.runback.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package socketio
import (
"errors"
"fmt"
"net/http"
eiot "github.com/oarkflow/socketio/engineio/transport"
siop "github.com/oarkflow/socketio/protocol"
siot "github.com/oarkflow/socketio/transport"
)
func autoConnect(v1 *ServerV1) func(transport eiot.Transporter, r *http.Request) {
return func(transport eiot.Transporter, r *http.Request) {
socketID, err := v1.tr().Add(transport)
if err != nil {
v1.tr().Send(socketID, serviceError(err), siop.WithType(siop.ErrorPacket.Byte()))
return
}
socket := siot.Socket{
Type: siop.ConnectPacket.Byte(),
Namespace: "/",
}
if err := v1.doConnectPacket(socketID, socket, sioRequest(r)); err != nil {
v1.tr().Send(socketID, serviceError(err), siop.WithType(siop.ErrorPacket.Byte()))
return
}
}
}
// runV1 are the callbacks that are used for version 1 of the server based on the
// receive of the transport and the packet type. This can be different for the
// different server versions.
func runV1(v1 *ServerV1) func(SocketID, *Request) error {
return func(socketID SocketID, req *Request) error {
for socket := range v1.tr().Receive(socketID) {
if err := doV1(v1, socketID, socket, req); err != nil {
return err
}
}
return nil
}
}
func doV1(v1 *ServerV1, socketID SocketID, socket siot.Socket, req *Request) error {
switch socket.Type {
case siop.ConnectPacket.Byte():
if err := v1.doConnectPacket(socketID, socket, req); err != nil {
v1.tr().Send(socketID, serviceError(err), siop.WithType(siop.ErrorPacket.Byte()))
}
case siop.DisconnectPacket.Byte():
if err := v1.doDisconnectPacket(socketID, socket, req); err != nil {
if errors.Is(err, ErrOnDisconnectSocket) {
return nil
}
v1.tr().Send(socketID, serviceError(err), siop.WithType(siop.ErrorPacket.Byte()))
}
case siop.EventPacket.Byte():
if err := v1.doEventPacket(socketID, socket); err != nil {
v1.tr().Send(socketID, serviceError(err), siop.WithType(siop.ErrorPacket.Byte()))
}
case siop.AckPacket.Byte():
if err := v1.doAckPacket(socketID, socket); err != nil {
v1.tr().Send(socketID, serviceError(err), siop.WithType(siop.ErrorPacket.Byte()))
}
case siop.ErrorPacket.Byte():
if e, ok := socket.Data.(error); ok {
return e
}
default:
err := ErrUnexpectedPacketType.F(socket).KV(ver, "v1")
v1.tr().Send(socketID, serviceError(err), siop.WithType(siop.ErrorPacket.Byte()))
}
return nil
}
// doConnectPacket the function
func doConnectPacket(v1 *ServerV1) func(SocketID, siot.Socket, *Request) error {
return func(socketID SocketID, socket siot.Socket, req *Request) (err error) {
unlock := v1.r()
tr := v1.tr()
unlock()
tr.Join(socket.Namespace, socketID, socketID.Room(socketIDPrefix))
v1.setPrefix()
v1.setSocketID(socketID)
v1.setNsp(socket.Namespace)
if fn, ok := v1.onConnect[socket.Namespace]; ok {
tr.Send(socketID, nil, siop.WithType(byte(siop.ConnectPacket)), siop.WithNamespace(socket.Namespace))
return fn(&SocketV1{inSocketV1: v1.inSocketV1.clone(), req: req, Connected: true})
}
return ErrOnConnectSocket
}
}
func doDisconnectPacket(v1 *ServerV1) func(SocketID, siot.Socket, *Request) error {
return func(socketID SocketID, socket siot.Socket, req *Request) (err error) {
if fn, ok := v1.events[socket.Namespace][OnDisconnectEvent][socketID]; ok {
v1.tr().Leave(socket.Namespace, socketID, socketIDPrefix+socketID.String())
v1.tr().Disconnect(socketID)
return fn.Callback("client namespace disconnect")
}
// for any socket id at the io. (server) level...
if fn, ok := v1.events[socket.Namespace][OnDisconnectEvent][serverEvent]; ok {
v1.tr().Leave(socket.Namespace, socketID, socketIDPrefix+socketID.String())
v1.tr().Disconnect(socketID)
return fn.Callback("client namespace disconnect")
}
return ErrOnDisconnectSocket
}
}
func doEventPacket(v1 *ServerV1) func(SocketID, siot.Socket) error {
return func(socketID SocketID, socket siot.Socket) (err error) {
type callbackAck interface {
CallbackAck(...interface{}) []interface{}
}
switch data := socket.Data.(type) {
case []interface{}:
event, ok := data[0].(string)
if !ok {
return ErrUnknownEventName
}
if socket.Type != siop.DisconnectPacket.Byte() {
data = data[1:]
}
if fn, ok := v1.events[socket.Namespace][event][socketID]; ok {
if socket.AckID > 0 {
if fn, ok := fn.(callbackAck); ok {
vals := fn.CallbackAck(data...)
return v1.tr().Send(socketID, vals, siop.WithNamespace(socket.Namespace), siop.WithAckID(socket.AckID), siop.WithType(byte(siop.AckPacket)))
}
}
return fn.Callback(data...)
}
if fn, ok := v1.events[socket.Namespace][event][serverEvent]; ok {
if socket.AckID > 0 {
if fn, ok := fn.(callbackAck); ok {
vals := fn.CallbackAck(data...)
return v1.tr().Send(socketID, vals, siop.WithNamespace(socket.Namespace), siop.WithAckID(socket.AckID), siop.WithType(byte(siop.AckPacket)))
}
}
return fn.Callback(data...)
}
case []string:
event := data[0]
if socket.Type != siop.DisconnectPacket.Byte() {
data = data[1:]
}
if fn, ok := v1.events[socket.Namespace][event][socketID]; ok {
return fn.Callback(stoi(data)...)
}
if fn, ok := v1.events[socket.Namespace][event][serverEvent]; ok {
return fn.Callback(stoi(data)...)
}
}
return ErrUnexpectedData.F(socket.Data).KV("do", "eventPacket")
}
}
func doAckPacket(v1 *ServerV1) func(SocketID, siot.Socket) error {
return func(socketID SocketID, socket siot.Socket) (err error) {
event := fmt.Sprintf("%s%d", ackIDEventPrefix, socket.AckID)
switch data := socket.Data.(type) {
case []interface{}:
if fn, ok := v1.events[socket.Namespace][event][socketID]; ok {
return fn.Callback(data...)
}
case []string:
event := data[0]
if fn, ok := v1.events[socket.Namespace][event][socketID]; ok {
err = fn.Callback(stoi(data)...)
}
default:
return ErrUnexpectedData.F(data).KV("do", "ackPacket")
}
return err
}
}