Now we delete users after closing connection

This commit is contained in:
qowevisa 2024-06-07 12:36:44 +03:00
parent e29cc04203
commit a4d6137a5a
2 changed files with 18 additions and 0 deletions

View File

@ -55,6 +55,8 @@ func main() {
func handleClient(conn net.Conn) {
defer conn.Close()
buf := make([]byte, 512)
var registeredID uint16
var isRegistered bool
ask, err := com.ServerAskClientAboutNickname()
if err != nil {
log.Printf("ERROR: %#v\n", err)
@ -104,6 +106,8 @@ func handleClient(conn net.Conn) {
continue
}
conn.Write(answ)
isRegistered = true
registeredID = id
}
case com.ID_CLIENT_SEND_SERVER_LINK:
default:
@ -111,4 +115,7 @@ func handleClient(conn net.Conn) {
// Handle
}
log.Println("server: conn: closed")
if isRegistered {
userCenter.DeleteIfHaveOne(registeredID)
}
}

View File

@ -49,6 +49,17 @@ func (u *UserCenter) AddUser(name string) (uint16, error) {
return ret, nil
}
func (u *UserCenter) DeleteIfHaveOne(id uint16) {
name, found := u.UsersITOS[id]
if !found {
log.Printf("User with %d id is not found; Can not delete\n", id)
return
}
delete(u.UsersITOS, id)
delete(u.UsersSTOI, name)
log.Printf("User with %s name and %d id was found; User is deleted\n", name, id)
}
func (u *UserCenter) GetID(name string) (uint16, error) {
id, have := u.UsersSTOI[name]
if !have {