diff --git a/cmd/client/main.go b/cmd/client/main.go index cc53be1..e7ed85f 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -99,6 +99,15 @@ func showMenu(name string, ms process.Ms) { case 2: sendMessage(ms.Conn) case 4: + err := process.WriteConn(ms.Conn, message.Message{ + Type: "offline", + Code: 0, + Msg: "", + Data: nil, + }) + if err != nil { + fmt.Println(err) + } menu1 <- 1 return } @@ -167,7 +176,10 @@ func handleMsg() { //处理 fmt.Printf("%d\t%s\n", pre.Id, pre.Name) } menu2 <- 1 + case "notice": + fmt.Printf("\n系统:%s\n", c.Msg.Msg) } + } } } diff --git a/dao/dao.go b/dao/dao.go index 47da291..e009136 100644 --- a/dao/dao.go +++ b/dao/dao.go @@ -15,6 +15,25 @@ func init() { Ctx = context.Background() } +func UserInfo(id int) *user.User { + rd := rdm.GetRdm() + ctx := context.Background() + key := user.GetUserKey(id) + u := rd.HGetAll(ctx, key).Val() + return map2user(u) +} + +func map2user(hash map[string]string) *user.User { + id, _ := strconv.Atoi(hash["id"]) + sex, _ := strconv.Atoi(hash["sex"]) + return &user.User{ + Id: id, + Name: hash["name"], + Sex: int8(sex), + Password: hash["password"], + } +} + func AddUser(u user.User) error { getRdm := rdm.GetRdm() if u.Name == "" { diff --git a/lib/server.go b/lib/server.go index 23da1cc..a1b0f79 100644 --- a/lib/server.go +++ b/lib/server.go @@ -95,6 +95,15 @@ func (server *Server) Process(conn net.Conn) { fmt.Println(read) } +func notice(message2 message.Message) { + for _, userProcess := range process.GetOnlineUsers() { + err := process.WriteConn(userProcess.Conn, message2) + if err != nil { + fmt.Println(err) + } + } +} + func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) { login, err := server.Login(s.Uid, s.Pw) re := message.Message{ @@ -112,6 +121,12 @@ func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) { r.Msg = login.Name r.Error = "" r.User = *login + notice(message.Message{ + Type: "notice", + Code: 0, + Msg: fmt.Sprintf("%s已上线", login.Name), + Data: nil, + }) process.Push(&process.UserProcess{ Uid: login.Id, Conn: conn, @@ -180,6 +195,20 @@ func (s *Server) processConn() { if err != nil { fmt.Println(err) } + case "offline": + id := process.Disconnect(c.Conn) + u := dao.UserInfo(id) + for _, userProcess := range process.GetOnlineUsers() { + err := process.WriteConn(userProcess.Conn, message.Message{ + Type: "notice", + Code: 0, + Msg: "用户" + u.Name + "已下线", + Data: nil, + }) + if err != nil { + fmt.Println(err) + } + } } } } diff --git a/process/userManager.go b/process/userManager.go index bf32ae0..3d15440 100644 --- a/process/userManager.go +++ b/process/userManager.go @@ -1,6 +1,7 @@ package process import ( + "fmt" "net" ) @@ -31,11 +32,13 @@ func Del(id int) { delete(onlineUsers, id) } -func Disconnect(conn net.Conn) { +func Disconnect(conn net.Conn) int { for u, process := range GetOnlineUsers() { if conn.RemoteAddr() == process.Conn.RemoteAddr() { + fmt.Println("用户", u, "已下线") delete(onlineUsers, u) - break + return u } } + return 0 }