diff --git a/cmd/client/main.go b/cmd/client/main.go index e7ed85f..0767922 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -8,6 +8,8 @@ import ( "fmt" "net" "os" + "strconv" + "strings" ) var menu1 = make(chan int) @@ -42,7 +44,8 @@ func sendMessage(conn net.Conn) { for { fmt.Print("请输入内容: ") var i string - _, err := fmt.Scanf("%s", &i) + in := bufio.NewReader(os.Stdin) + _, err := fmt.Fscanf(in, "%s", &i) if err != nil { fmt.Println(err) return @@ -50,15 +53,20 @@ 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 + } err = process.WriteConn(conn, message.Message{ Type: "user_message", Code: 1, Msg: "", Data: message.UserMessage{ - TotUid: 0, + TotUid: uid, FromUid: thisUser.Id, FromUserName: thisUser.Name, - Msg: i, + Msg: a[1], }, }) if err != nil { @@ -119,7 +127,7 @@ func addUser(conn net.Conn) { fmt.Print("请输入姓名、密码、性别(1男2女)空格隔开:") var name, pw string var sex int8 - _, err := fmt.Scanf("%s %s %d", &name, &pw, &sex) + _, err := fmt.Fscanf(bufio.NewReader(os.Stdin), "%s %s %d", &name, &pw, &sex) if err != nil { fmt.Println(err) return @@ -168,7 +176,7 @@ func handleMsg() { //处理 menu1 <- 1 case "user_message": m := c.Msg.Data.(*message.UserMessage) - fmt.Printf("\r%s: %s\n", m.FromUserName, m.Msg) + fmt.Printf("\r%s %s\n%s\n", m.FromUserName, m.Datetime, m.Msg) case "online_users": list := c.Msg.Data.(*message.UsersPres) fmt.Printf("%s\t%s\n", "id", "昵称") @@ -179,7 +187,6 @@ func handleMsg() { //处理 case "notice": fmt.Printf("\n系统:%s\n", c.Msg.Msg) } - } } } diff --git a/lib/server.go b/lib/server.go index a1b0f79..6c095ec 100644 --- a/lib/server.go +++ b/lib/server.go @@ -13,6 +13,7 @@ import ( "io/ioutil" "net" "strconv" + "time" ) type Server struct { @@ -141,6 +142,11 @@ func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) { } func (s *Server) processConn() { + defer func() { + if r := recover(); r != nil { + fmt.Println(r) + } + }() for { select { case c := <-Rchan: @@ -172,6 +178,44 @@ func (s *Server) processConn() { case "user_message": data := c.Msg.Data.(*message.UserMessage) if data.TotUid > 0 { + to := process.Get(data.TotUid) + if to == nil { + to = process.Get(data.FromUid) + if to == nil { + break + } + err := process.WriteConn(to.Conn, message.Message{ + Type: "user_message", + 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"), + }, + }) + if err != nil { + fmt.Println(err) + } + break + } + err := process.WriteConn(to.Conn, message.Message{ + Type: "user_message", + Code: 0, + Msg: "", + Data: message.UserMessage{ + FromUid: data.FromUid, + FromUserName: data.FromUserName, + TotUid: data.TotUid, + Msg: data.Msg, + Datetime: time.Now().Format("2006-01-02 15:04:05"), + }, + }) + if err != nil { + fmt.Println(err) + } } else { fmt.Println(data.FromUserName, ":", data.Msg) @@ -225,65 +269,6 @@ func (server *Server) read(conn net.Conn) { process.Read(conn, Rchan) - /*for { - var info = make([]byte, 65536) - i, err := conn.Read(info) - if err != nil { - fmt.Println(err, "errrrrrrrrrrrrr") - return - } - msg := message.Message{} - err = json.Unmarshal(info[:i], &msg) - if err != nil { - fmt.Println(err) - } - t := message.MsgType[msg.Type] - mm := message.Message{} - mm.Data = reflect.New(t).Interface() - err = json.Unmarshal(info[:i], &mm) - if err != nil { - fmt.Println(err) - } - switch msg.Type { - case "login_send": - if d,ok:=mm.Data.(*message.LoginS);ok{ - _, err := server.do(d,conn) - if err != nil { - fmt.Println(err) - return - } - } - - } - - /*switch reflect.TypeOf(mm.Data){ - case reflect.TypeOf(&LoginS{}): - x :=mm.Data.(*LoginS) - e := s.Login(x.Uid,x.Pw) - re := Message{ - Type: "login_response", - Data: nil, - } - if e != nil { - re.Data=Correspond{ - Code: 0, - Msg: "login fail", - Error: "pw fail or user not exist", - } - }else{ - re.Data=Correspond{ - Code: 1, - Msg: "success", - Error: "", - } - } - m, _ :=json.Marshal(re) - _, err := conn.Write(m) - if err != nil { - fmt.Println(err) - } - }*/ - } func (server *Server) Start() { diff --git a/message/message.go b/message/message.go index 00a385a..951128b 100644 --- a/message/message.go +++ b/message/message.go @@ -35,6 +35,7 @@ type UserMessage struct { FromUserName string TotUid int Msg string + Datetime string } type LoginS struct {