add DelSession

This commit is contained in:
ssp97 2024-08-22 20:35:38 +08:00
parent 9069d1293c
commit c3e7360019
2 changed files with 11 additions and 1 deletions

View File

@ -67,6 +67,7 @@ func (h *MqttServerHook) OnDisconnect(cl *mqttServerV2.Client, err error, expire
session := GetSession(cl.ID) session := GetSession(cl.ID)
MqttClientDisconnect(session) MqttClientDisconnect(session)
DelSession(session.ClientId)
} }
@ -106,7 +107,12 @@ func (h *MqttServerHook) OnPublished(cl *mqttServerV2.Client, pk packets.Packet)
// OnConnectAuthenticate returns true/allowed for all requests. // OnConnectAuthenticate returns true/allowed for all requests.
func (h *MqttServerHook) OnConnectAuthenticate(cl *mqttServerV2.Client, pk packets.Packet) bool { func (h *MqttServerHook) OnConnectAuthenticate(cl *mqttServerV2.Client, pk packets.Packet) bool {
h.Log.Info("Auth", pk.Connect.Username, pk.Connect.Password) h.Log.Info("Auth", pk.Connect.Username, pk.Connect.Password)
session := CreateSession(cl.ID, pk.Connect.Username, pk.Connect.Password) session := GetSession(cl.ID)
if session != nil {
MqttClientDisconnect(session)
DelSession(session.ClientId)
}
session = CreateSession(cl.ID, pk.Connect.Username, pk.Connect.Password)
err := MqttClientConnect(session) err := MqttClientConnect(session)
if err != nil { if err != nil {
return false return false

View File

@ -23,3 +23,7 @@ func CreateSession(mqttId string, user, passwd []byte) *Session {
func GetSession(mqttId string) *Session { func GetSession(mqttId string) *Session {
return Sessions[mqttId] return Sessions[mqttId]
} }
func DelSession(mqttId string) {
delete(Sessions, mqttId)
}