go-chat/process/userManager.go

45 lines
644 B
Go
Raw Permalink Normal View History

2021-04-28 08:16:18 +00:00
package process
import (
2021-04-28 09:29:38 +00:00
"fmt"
2021-04-28 08:16:18 +00:00
"net"
)
type UserProcess struct {
Uid int
Conn net.Conn
}
var onlineUsers = make(map[int]*UserProcess)
func GetOnlineUsers() map[int]*UserProcess {
return onlineUsers
}
func Push(process *UserProcess) {
onlineUsers[process.Uid] = process
}
func Get(id int) *UserProcess {
u, ok := onlineUsers[id]
if ok {
return u
}
return nil
}
func Del(id int) {
delete(onlineUsers, id)
}
2021-04-28 09:29:38 +00:00
func Disconnect(conn net.Conn) int {
2021-04-28 08:16:18 +00:00
for u, process := range GetOnlineUsers() {
if conn.RemoteAddr() == process.Conn.RemoteAddr() {
2021-04-28 09:29:38 +00:00
fmt.Println("用户", u, "已下线")
2021-05-07 10:08:46 +00:00
Del(u)
2021-04-28 09:29:38 +00:00
return u
2021-04-28 08:16:18 +00:00
}
}
2021-04-28 09:29:38 +00:00
return 0
2021-04-28 08:16:18 +00:00
}