-
Notifications
You must be signed in to change notification settings - Fork 14
/
adapter-type.go
154 lines (114 loc) 路 4.82 KB
/
adapter-type.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
package socket
import (
"time"
"github.com/zishang520/engine.io-go-parser/packet"
"github.com/zishang520/engine.io/v2/events"
"github.com/zishang520/engine.io/v2/types"
"github.com/zishang520/socket.io-go-parser/v2/parser"
)
type (
// A public ID, sent by the server at the beginning of the Socket.IO session and which can be used for private messaging
SocketId string
// A private ID, sent by the server at the beginning of the Socket.IO session and used for connection state recovery
// upon reconnection
PrivateSessionId string
// we could extend the Room type to "string", but that would be a breaking change
// Related: https://github.com/socketio/socket.io-redis-adapter/issues/418
Room string
WriteOptions struct {
packet.Options
Volatile bool `json:"volatile" mapstructure:"volatile" msgpack:"volatile"`
PreEncoded bool `json:"preEncoded" mapstructure:"preEncoded" msgpack:"preEncoded"`
}
BroadcastFlags struct {
WriteOptions
Local bool `json:"local" mapstructure:"local" msgpack:"local"`
Broadcast bool `json:"broadcast" mapstructure:"broadcast" msgpack:"broadcast"`
Binary bool `json:"binary" mapstructure:"binary" msgpack:"binary"`
Timeout *time.Duration `json:"timeout,omitempty" mapstructure:"timeout,omitempty" msgpack:"timeout,omitempty"`
ExpectSingleResponse bool `json:"expectSingleResponse" mapstructure:"expectSingleResponse" msgpack:"expectSingleResponse"`
}
BroadcastOptions struct {
Rooms *types.Set[Room]
Except *types.Set[Room]
Flags *BroadcastFlags `json:"flags,omitempty" mapstructure:"flags,omitempty" msgpack:"flags,omitempty"`
}
SessionToPersist struct {
Sid SocketId `json:"sid" mapstructure:"sid" msgpack:"sid"`
Pid PrivateSessionId `json:"pid" mapstructure:"pid" msgpack:"pid"`
Rooms *types.Set[Room]
Data any `json:"data" mapstructure:"data" msgpack:"data"`
}
Session struct {
*SessionToPersist
MissedPackets []any `json:"missedPackets" mapstructure:"missedPackets" msgpack:"missedPackets"`
}
PersistedPacket struct {
Id string `json:"id" mapstructure:"id" msgpack:"id"`
EmittedAt int64 `json:"emittedAt" mapstructure:"emittedAt" msgpack:"emittedAt"`
Data any `json:"data" mapstructure:"data" msgpack:"data"`
Opts *BroadcastOptions `json:"opts,omitempty" mapstructure:"opts,omitempty" msgpack:"opts,omitempty"`
}
SessionWithTimestamp struct {
*SessionToPersist
DisconnectedAt int64 `json:"disconnectedAt" mapstructure:"disconnectedAt" msgpack:"disconnectedAt"`
}
Adapter interface {
events.EventEmitter
// #prototype
Prototype(Adapter)
Proto() Adapter
Rooms() *types.Map[Room, *types.Set[SocketId]]
Sids() *types.Map[SocketId, *types.Set[Room]]
Nsp() NamespaceInterface
// Construct() should be called after calling Prototype()
Construct(NamespaceInterface)
// To be overridden
Init()
// To be overridden
Close()
// Returns the number of Socket.IO servers in the cluster
ServerCount() int64
// Adds a socket to a list of room.
AddAll(SocketId, *types.Set[Room])
// Removes a socket from a room.
Del(SocketId, Room)
// Removes a socket from all rooms it's joined.
DelAll(SocketId)
// Broadcasts a packet.
//
// Options:
// - `Flags` {*BroadcastFlags} flags for this packet
// - `Except` {*types.Set[Room]} sids that should be excluded
// - `Rooms` {*types.Set[Room]} list of rooms to broadcast to
Broadcast(*parser.Packet, *BroadcastOptions)
// Broadcasts a packet and expects multiple acknowledgements.
//
// Options:
// - `Flags` {*BroadcastFlags} flags for this packet
// - `Except` {*types.Set[Room]} sids that should be excluded
// - `Rooms` {*types.Set[Room]} list of rooms to broadcast to
BroadcastWithAck(*parser.Packet, *BroadcastOptions, func(uint64), func([]any, error))
// Gets a list of sockets by sid.
Sockets(*types.Set[Room]) *types.Set[SocketId]
// Gets the list of rooms a given socket has joined.
SocketRooms(SocketId) *types.Set[Room]
// Returns the matching socket instances
FetchSockets(*BroadcastOptions) func(func([]SocketDetails, error))
// Makes the matching socket instances join the specified rooms
AddSockets(*BroadcastOptions, []Room)
// Makes the matching socket instances leave the specified rooms
DelSockets(*BroadcastOptions, []Room)
// Makes the matching socket instances disconnect
DisconnectSockets(*BroadcastOptions, bool)
// Send a packet to the other Socket.IO servers in the cluster
ServerSideEmit([]any) error
// Save the client session in order to restore it upon reconnection.
PersistSession(*SessionToPersist)
// Restore the session and find the packets that were missed by the client.
RestoreSession(PrivateSessionId, string) (*Session, error)
}
AdapterConstructor interface {
New(NamespaceInterface) Adapter
}
)