Skip to content

Commit ea5aa32

Browse files
authored
Update the SIO disconnect packet functionality (#40)
The functionality around disconnecting a SIO packet was broken. These changes fix that and allow namespaced and non-namespaced disconnects to happen.
1 parent ec713d4 commit ea5aa32

File tree

4 files changed

+44
-4
lines changed

4 files changed

+44
-4
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
protocol/socket.io-protocol
2+
testing

Diff for: callback/callback.go

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ func (fn ErrorWrap) Callback(data ...interface{}) error { return fn() }
1515
func (ErrorWrap) Serialize() (string, error) { return "", ErrStubSerialize }
1616
func (ErrorWrap) Unserialize(string) error { return ErrStubUnserialize }
1717

18+
type FuncAny func(...interface{}) error
19+
20+
func (fn FuncAny) Callback(v ...interface{}) error {
21+
return fn(v...)
22+
}
23+
func (FuncAny) Serialize() (string, error) { return "", ErrStubSerialize }
24+
func (FuncAny) Unserialize(string) error { return ErrStubUnserialize }
25+
1826
type FuncString func(string)
1927

2028
func (fn FuncString) Callback(v ...interface{}) error {

Diff for: serialize/serialize.go

+14
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func stringify(val interface{}) string {
5050
}
5151

5252
var (
53+
AnyParam = _anyWrap{Any(nil)}
5354
BinParam = _binaryWrap{Binary(nil)}
5455
ErrParam = _errorWrap{Error(nil)}
5556
F64Param = _float64Param{Float64(0)}
@@ -59,6 +60,19 @@ var (
5960
UintParam = _uintParam{Uinteger(0)}
6061
)
6162

63+
type (
64+
_any struct{ a interface{} }
65+
_anyWrap struct{ SerializableWrap }
66+
)
67+
68+
func Any(v interface{}) *_any { return &_any{v} }
69+
func (x *_any) String() (str string) { str, _ = x.Serialize(); return }
70+
func (x *_any) Serialize() (str string, err error) { return "", ErrSerializableBinary }
71+
func (x *_any) Unserialize(str string) (err error) { return ErrSerializableBinary }
72+
func (x *_any) Interface() (v interface{}) { return x.a }
73+
func (x _anyWrap) Unserialize(string) error { return nil }
74+
func (x _anyWrap) String() string { return "" }
75+
6276
type (
6377
_binary struct{ r io.Reader }
6478
_binaryWrap struct{ SerializableWrap }

Diff for: server.v1.runback.go

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package socketio
22

33
import (
4+
"errors"
45
"fmt"
56
"net/http"
67

@@ -57,6 +58,9 @@ func doV1(v1 *ServerV1, socketID SocketID, socket siot.Socket, req *Request) err
5758
}
5859
case siop.DisconnectPacket.Byte():
5960
if err := v1.doDisconnectPacket(socketID, socket, req); err != nil {
61+
if errors.Is(err, ErrBadOnDisconnectSocket) {
62+
return nil
63+
}
6064
v1.tr().Send(socketID, serviceError(err), siop.WithType(siop.ErrorPacket.Byte()))
6165
}
6266
case siop.EventPacket.Byte():
@@ -100,6 +104,11 @@ func doDisconnectPacket(v1 *ServerV1) func(SocketID, siot.Socket, *Request) erro
100104
v1.tr().Leave(socket.Namespace, socketID, socketIDPrefix+socketID.String())
101105
return fn.Callback("client namespace disconnect")
102106
}
107+
// for any socket id at the io. (server) level...
108+
if fn, ok := v1.events[socket.Namespace][OnDisconnectEvent][serverEvent]; ok {
109+
v1.tr().Leave(socket.Namespace, socketID, socketIDPrefix+socketID.String())
110+
return fn.Callback("client namespace disconnect")
111+
}
103112
return ErrBadOnDisconnectSocket
104113
}
105114
}
@@ -112,20 +121,27 @@ func doEventPacket(v1 *ServerV1) func(SocketID, siot.Socket) error {
112121
if !ok {
113122
return ErrBadEventName
114123
}
124+
if socket.Type != siop.DisconnectPacket.Byte() {
125+
data = data[1:]
126+
}
115127

116128
if fn, ok := v1.events[socket.Namespace][event][socketID]; ok {
117-
return fn.Callback(data[1:]...)
129+
return fn.Callback(data...)
118130
}
119131
if fn, ok := v1.events[socket.Namespace][event][serverEvent]; ok {
120-
return fn.Callback(data[1:]...)
132+
return fn.Callback(data...)
121133
}
122134
case []string:
123135
event := data[0]
136+
if socket.Type != siop.DisconnectPacket.Byte() {
137+
data = data[1:]
138+
}
139+
124140
if fn, ok := v1.events[socket.Namespace][event][socketID]; ok {
125-
return fn.Callback(stoi(data[1:])...)
141+
return fn.Callback(stoi(data)...)
126142
}
127143
if fn, ok := v1.events[socket.Namespace][event][serverEvent]; ok {
128-
return fn.Callback(stoi(data[1:])...)
144+
return fn.Callback(stoi(data)...)
129145
}
130146
}
131147
return ErrInvalidData.F(fmt.Sprintf("type %s", socket.Data)).KV("do", "eventPacket")

0 commit comments

Comments
 (0)