-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.v2.runback.go
95 lines (85 loc) · 2.74 KB
/
server.v2.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
package socketio
import (
siop "github.com/njones/socketio/protocol"
siot "github.com/njones/socketio/transport"
)
func doConnectPacketV2(v2 *ServerV2) func(SocketID, siot.Socket, *Request) error {
return func(socketID SocketID, socket siot.Socket, req *Request) (err error) {
unlock := v2.prev.r()
tr := v2.tr()
unlock()
tr.Join(socket.Namespace, socketID, socketID.Room(socketIDPrefix))
v2.setPrefix()
v2.setSocketID(socketID)
v2.setNsp(socket.Namespace)
if fn, ok := v2.onConnect[socket.Namespace]; ok {
tr.Send(socketID, nil, siop.WithType(byte(siop.ConnectPacket)), siop.WithNamespace(socket.Namespace))
return fn(&SocketV2{inSocketV2: v2.inSocketV2.clone(), req: req})
}
return ErrOnConnectSocket
}
}
func doBinaryEventPacket(v2 *ServerV2) func(SocketID, siot.Socket) error {
v1 := v2.prev
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 ErrUnknownBinaryEventName.F(data)
}
if fn, ok := v1.events[socket.Namespace][event][socketID]; ok {
if socket.AckID > 0 {
if fn, ok := fn.(callbackAck); ok {
vals := fn.CallbackAck(data[1:]...)
return v1.tr().Send(socketID, vals, siop.WithNamespace(socket.Namespace), siop.WithAckID(socket.AckID), siop.WithType(byte(siop.BinaryAckPacket)))
}
}
return fn.Callback(data[1:]...)
}
if fn, ok := v1.events[socket.Namespace][event][serverEvent]; ok {
if socket.AckID > 0 {
if fn, ok := fn.(callbackAck); ok {
vals := fn.CallbackAck(data[1:]...)
return v1.tr().Send(socketID, vals, siop.WithNamespace(socket.Namespace), siop.WithAckID(socket.AckID), siop.WithType(byte(siop.BinaryAckPacket)))
}
}
return fn.Callback(data[1:]...)
}
case []string:
event := data[0]
if fn, ok := v1.events[socket.Namespace][event][socketID]; ok {
err = fn.Callback(stoi(data[1:])...)
}
default:
return ErrUnexpectedBinaryData.F(socket.Data)
}
return err
}
}
func runV2(v2 *ServerV2) func(SocketID, *Request) error {
return func(socketID SocketID, req *Request) error {
unlock := v2.prev.r()
tr := v2.tr()
unlock()
for socket := range tr.Receive(socketID) {
if err := doV2(v2, socketID, socket, req); err != nil {
return err
}
}
return nil
}
}
func doV2(v2 *ServerV2, socketID SocketID, socket siot.Socket, req *Request) error {
switch socket.Type {
case siop.BinaryEventPacket.Byte():
if err := v2.doBinaryEventPacket(socketID, socket); err != nil {
v2.tr().Send(socketID, serviceError(err), siop.WithType(byte(siop.ErrorPacket)))
}
return nil
}
return doV1(v2.prev, socketID, socket, req)
}