diff --git a/cmd/client/main.go b/cmd/client/main.go index 0767922..69c019b 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -8,8 +8,6 @@ import ( "fmt" "net" "os" - "strconv" - "strings" ) var menu1 = make(chan int) @@ -41,6 +39,16 @@ func login(conn net.Conn) error { } func sendMessage(conn net.Conn) { + id := 0 + for { + fmt.Print("请输入用户id(0为所有):") + _, err := fmt.Fscanf(bufio.NewReader(os.Stdin), "%d", &id) + if err != nil { + fmt.Println(err) + } else { + break + } + } for { fmt.Print("请输入内容: ") var i string @@ -53,11 +61,7 @@ func sendMessage(conn net.Conn) { if i == "exit" { break } - a := strings.Split(i, "|") - uid := 0 - if id, ok := strconv.Atoi(a[0]); ok == nil { - uid = id - } + uid := id err = process.WriteConn(conn, message.Message{ Type: "user_message", Code: 1, @@ -66,7 +70,7 @@ func sendMessage(conn net.Conn) { TotUid: uid, FromUid: thisUser.Id, FromUserName: thisUser.Name, - Msg: a[1], + Msg: i, }, }) if err != nil { @@ -83,7 +87,7 @@ func showMenu(name string, ms process.Ms) { fmt.Printf("\t\t\t 1.显示在线用户列表\n") fmt.Printf("\t\t\t 2.发送消息\n") fmt.Printf("\t\t\t 3.信息列表\n") - fmt.Printf("\t\t\t 4.退出\n") + fmt.Printf("\t\t\t 4.返回上一级菜单\n") fmt.Printf("\t\t\t 请选择(1-4):") var k int _, err := fmt.Scanf("%d", &k) @@ -186,6 +190,9 @@ func handleMsg() { //处理 menu2 <- 1 case "notice": fmt.Printf("\n系统:%s\n", c.Msg.Msg) + case "all_users": + m := c.Msg.Data.(*message.AllUser) + fmt.Printf("\r%s(%d) %s\n%s\n", m.FromUserName, m.FromUid, m.DateTime, m.Msg) } } } diff --git a/lib/server.go b/lib/server.go index 6c095ec..c31d5d4 100644 --- a/lib/server.go +++ b/lib/server.go @@ -177,6 +177,7 @@ func (s *Server) processConn() { } case "user_message": data := c.Msg.Data.(*message.UserMessage) + now := time.Now().Format("2006-01-02 15:04:05") if data.TotUid > 0 { to := process.Get(data.TotUid) if to == nil { @@ -184,17 +185,12 @@ func (s *Server) processConn() { if to == nil { break } + toU := dao.UserInfo(data.TotUid) err := process.WriteConn(to.Conn, message.Message{ - Type: "user_message", + Type: "notice", Code: 0, - Msg: "user had offline", - Data: message.UserMessage{ - FromUid: 0, - FromUserName: "系统", - TotUid: data.FromUid, - Msg: "user had offline", - Datetime: time.Now().Format("2006-01-02 15:04:05"), - }, + Msg: "用户" + toU.Name + "已下线", + Data: nil, }) if err != nil { fmt.Println(err) @@ -210,7 +206,7 @@ func (s *Server) processConn() { FromUserName: data.FromUserName, TotUid: data.TotUid, Msg: data.Msg, - Datetime: time.Now().Format("2006-01-02 15:04:05"), + Datetime: now, }, }) if err != nil { @@ -219,6 +215,22 @@ func (s *Server) processConn() { } else { fmt.Println(data.FromUserName, ":", data.Msg) + for _, userProcess := range process.GetOnlineUsers() { + err := process.WriteConn(userProcess.Conn, message.Message{ + Type: "all_users", + Code: 0, + Msg: "", + Data: message.AllUser{ + FromUid: data.FromUid, + FromUserName: data.FromUserName, + Msg: data.Msg, + DateTime: now, + }, + }) + if err != nil { + fmt.Println(err) + } + } } case "online_users": all := process.GetOnlineUsers() @@ -262,7 +274,19 @@ func (server *Server) read(conn net.Conn) { defer func(conn net.Conn) { err := conn.Close() if err != nil { - process.Disconnect(conn) + id := process.Disconnect(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) + } + } fmt.Println(err, "ssssssssssssssss") } }(conn) diff --git a/message/message.go b/message/message.go index 951128b..808dfcf 100644 --- a/message/message.go +++ b/message/message.go @@ -11,6 +11,7 @@ var MsgType = map[string]reflect.Type{ "add_user": reflect.TypeOf(&user.User{}).Elem(), "user_message": reflect.TypeOf(&UserMessage{}).Elem(), "online_users": reflect.TypeOf(&UsersPres{}).Elem(), + "all_users": reflect.TypeOf(&AllUser{}).Elem(), } type Message struct { @@ -21,6 +22,13 @@ type Message struct { Data interface{} `json:"data"` } +type AllUser struct { + FromUid int + FromUserName string + Msg string + DateTime string +} + type UsersPres struct { Data []UserPre } diff --git a/process/process.go b/process/process.go index 59c1a98..5869b13 100644 --- a/process/process.go +++ b/process/process.go @@ -4,6 +4,7 @@ import ( "chat/message" "encoding/json" "fmt" + "io" "net" "reflect" ) @@ -41,7 +42,11 @@ func (t *Transfer) ReadConn(c chan Ms) { var info = t.Buf i, err := t.Conn.Read(info[:]) if err != nil { - fmt.Println(err, "errrrrrrrrrrrrr") + if err == io.EOF { + fmt.Println("连接已断开。。。。") + } else { + fmt.Println(err, "errrrrrrrrrrrrr") + } return } msg := message.Message{} diff --git a/process/userManager.go b/process/userManager.go index 3d15440..22fde7e 100644 --- a/process/userManager.go +++ b/process/userManager.go @@ -36,7 +36,7 @@ func Disconnect(conn net.Conn) int { for u, process := range GetOnlineUsers() { if conn.RemoteAddr() == process.Conn.RemoteAddr() { fmt.Println("用户", u, "已下线") - delete(onlineUsers, u) + Del(u) return u } }