mirror of
https://github.com/mochi-mqtt/server.git
synced 2025-10-05 00:02:52 +08:00
Add OnSessionEstablish hook (#247)
Co-authored-by: Derek Duncan <derekduncan@gmail.com>
This commit is contained in:
@@ -284,6 +284,7 @@ The function signatures for all the hooks and `mqtt.Hook` interface can be found
|
|||||||
| OnACLCheck | Called when a user attempts to publish or subscribe to a topic filter. As above. |
|
| OnACLCheck | Called when a user attempts to publish or subscribe to a topic filter. As above. |
|
||||||
| OnSysInfoTick | Called when the $SYS topic values are published out. |
|
| OnSysInfoTick | Called when the $SYS topic values are published out. |
|
||||||
| OnConnect | Called when a new client connects, may return an error or packet code to halt the client connection process. |
|
| OnConnect | Called when a new client connects, may return an error or packet code to halt the client connection process. |
|
||||||
|
| OnSessionEstablish | Called right after a new client connects and authenticates and right before the session is established and CONNACK is sent.
|
||||||
| OnSessionEstablished | Called when a new client successfully establishes a session (after OnConnect) |
|
| OnSessionEstablished | Called when a new client successfully establishes a session (after OnConnect) |
|
||||||
| OnDisconnect | Called when a client is disconnected for any reason. |
|
| OnDisconnect | Called when a client is disconnected for any reason. |
|
||||||
| OnAuthPacket | Called when an auth packet is received. It is intended to allow developers to create their own mqtt v5 Auth Packet handling mechanisms. Allows packet modification. |
|
| OnAuthPacket | Called when an auth packet is received. It is intended to allow developers to create their own mqtt v5 Auth Packet handling mechanisms. Allows packet modification. |
|
||||||
|
16
hooks.go
16
hooks.go
@@ -25,6 +25,7 @@ const (
|
|||||||
OnConnectAuthenticate
|
OnConnectAuthenticate
|
||||||
OnACLCheck
|
OnACLCheck
|
||||||
OnConnect
|
OnConnect
|
||||||
|
OnSessionEstablish
|
||||||
OnSessionEstablished
|
OnSessionEstablished
|
||||||
OnDisconnect
|
OnDisconnect
|
||||||
OnAuthPacket
|
OnAuthPacket
|
||||||
@@ -76,6 +77,7 @@ type Hook interface {
|
|||||||
OnACLCheck(cl *Client, topic string, write bool) bool
|
OnACLCheck(cl *Client, topic string, write bool) bool
|
||||||
OnSysInfoTick(*system.Info)
|
OnSysInfoTick(*system.Info)
|
||||||
OnConnect(cl *Client, pk packets.Packet) error
|
OnConnect(cl *Client, pk packets.Packet) error
|
||||||
|
OnSessionEstablish(cl *Client, pk packets.Packet)
|
||||||
OnSessionEstablished(cl *Client, pk packets.Packet)
|
OnSessionEstablished(cl *Client, pk packets.Packet)
|
||||||
OnDisconnect(cl *Client, err error, expire bool)
|
OnDisconnect(cl *Client, err error, expire bool)
|
||||||
OnAuthPacket(cl *Client, pk packets.Packet) (packets.Packet, error)
|
OnAuthPacket(cl *Client, pk packets.Packet) (packets.Packet, error)
|
||||||
@@ -229,6 +231,16 @@ func (h *Hooks) OnConnect(cl *Client, pk packets.Packet) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnSessionEstablish is called right after a new client connects and authenticates and right before
|
||||||
|
// the session is established and CONNACK is sent.
|
||||||
|
func (h *Hooks) OnSessionEstablish(cl *Client, pk packets.Packet) {
|
||||||
|
for _, hook := range h.GetAll() {
|
||||||
|
if hook.Provides(OnSessionEstablish) {
|
||||||
|
hook.OnSessionEstablish(cl, pk)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// OnSessionEstablished is called when a new client establishes a session (after OnConnect).
|
// OnSessionEstablished is called when a new client establishes a session (after OnConnect).
|
||||||
func (h *Hooks) OnSessionEstablished(cl *Client, pk packets.Packet) {
|
func (h *Hooks) OnSessionEstablished(cl *Client, pk packets.Packet) {
|
||||||
for _, hook := range h.GetAll() {
|
for _, hook := range h.GetAll() {
|
||||||
@@ -713,6 +725,10 @@ func (h *HookBase) OnConnect(cl *Client, pk packets.Packet) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OnSessionEstablish is called right after a new client connects and authenticates and right before
|
||||||
|
// the session is established and CONNACK is sent.
|
||||||
|
func (h *HookBase) OnSessionEstablish(cl *Client, pk packets.Packet) {}
|
||||||
|
|
||||||
// OnSessionEstablished is called when a new client establishes a session (after OnConnect).
|
// OnSessionEstablished is called when a new client establishes a session (after OnConnect).
|
||||||
func (h *HookBase) OnSessionEstablished(cl *Client, pk packets.Packet) {}
|
func (h *HookBase) OnSessionEstablished(cl *Client, pk packets.Packet) {}
|
||||||
|
|
||||||
|
@@ -236,6 +236,7 @@ func TestHooksNonReturns(t *testing.T) {
|
|||||||
h.OnStarted()
|
h.OnStarted()
|
||||||
h.OnStopped()
|
h.OnStopped()
|
||||||
h.OnSysInfoTick(new(system.Info))
|
h.OnSysInfoTick(new(system.Info))
|
||||||
|
h.OnSessionEstablish(cl, packets.Packet{})
|
||||||
h.OnSessionEstablished(cl, packets.Packet{})
|
h.OnSessionEstablished(cl, packets.Packet{})
|
||||||
h.OnDisconnect(cl, nil, false)
|
h.OnDisconnect(cl, nil, false)
|
||||||
h.OnPacketSent(cl, packets.Packet{}, []byte{})
|
h.OnPacketSent(cl, packets.Packet{}, []byte{})
|
||||||
|
@@ -346,6 +346,8 @@ func (s *Server) attachClient(cl *Client, listener string) error {
|
|||||||
atomic.AddInt64(&s.Info.ClientsConnected, 1)
|
atomic.AddInt64(&s.Info.ClientsConnected, 1)
|
||||||
defer atomic.AddInt64(&s.Info.ClientsConnected, -1)
|
defer atomic.AddInt64(&s.Info.ClientsConnected, -1)
|
||||||
|
|
||||||
|
s.hooks.OnSessionEstablish(cl, pk)
|
||||||
|
|
||||||
sessionPresent := s.inheritClientSession(pk, cl)
|
sessionPresent := s.inheritClientSession(pk, cl)
|
||||||
s.Clients.Add(cl) // [MQTT-4.1.0-1]
|
s.Clients.Add(cl) // [MQTT-4.1.0-1]
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user