online offline notice

This commit is contained in:
xing 2021-04-28 17:29:38 +08:00
parent d92719e20e
commit 8c0fa2a217
4 changed files with 65 additions and 2 deletions

View File

@ -99,6 +99,15 @@ func showMenu(name string, ms process.Ms) {
case 2: case 2:
sendMessage(ms.Conn) sendMessage(ms.Conn)
case 4: case 4:
err := process.WriteConn(ms.Conn, message.Message{
Type: "offline",
Code: 0,
Msg: "",
Data: nil,
})
if err != nil {
fmt.Println(err)
}
menu1 <- 1 menu1 <- 1
return return
} }
@ -167,7 +176,10 @@ func handleMsg() { //处理
fmt.Printf("%d\t%s\n", pre.Id, pre.Name) fmt.Printf("%d\t%s\n", pre.Id, pre.Name)
} }
menu2 <- 1 menu2 <- 1
case "notice":
fmt.Printf("\n系统:%s\n", c.Msg.Msg)
} }
} }
} }
} }

View File

@ -15,6 +15,25 @@ func init() {
Ctx = context.Background() 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 { func AddUser(u user.User) error {
getRdm := rdm.GetRdm() getRdm := rdm.GetRdm()
if u.Name == "" { if u.Name == "" {

View File

@ -95,6 +95,15 @@ func (server *Server) Process(conn net.Conn) {
fmt.Println(read) 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) { func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) {
login, err := server.Login(s.Uid, s.Pw) login, err := server.Login(s.Uid, s.Pw)
re := message.Message{ re := message.Message{
@ -112,6 +121,12 @@ func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) {
r.Msg = login.Name r.Msg = login.Name
r.Error = "" r.Error = ""
r.User = *login r.User = *login
notice(message.Message{
Type: "notice",
Code: 0,
Msg: fmt.Sprintf("%s已上线", login.Name),
Data: nil,
})
process.Push(&process.UserProcess{ process.Push(&process.UserProcess{
Uid: login.Id, Uid: login.Id,
Conn: conn, Conn: conn,
@ -180,6 +195,20 @@ func (s *Server) processConn() {
if err != nil { if err != nil {
fmt.Println(err) 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)
}
}
} }
} }
} }

View File

@ -1,6 +1,7 @@
package process package process
import ( import (
"fmt"
"net" "net"
) )
@ -31,11 +32,13 @@ func Del(id int) {
delete(onlineUsers, id) delete(onlineUsers, id)
} }
func Disconnect(conn net.Conn) { func Disconnect(conn net.Conn) int {
for u, process := range GetOnlineUsers() { for u, process := range GetOnlineUsers() {
if conn.RemoteAddr() == process.Conn.RemoteAddr() { if conn.RemoteAddr() == process.Conn.RemoteAddr() {
fmt.Println("用户", u, "已下线")
delete(onlineUsers, u) delete(onlineUsers, u)
break return u
} }
} }
return 0
} }