diff --git a/Redis/redis.go b/Redis/redis.go deleted file mode 100644 index ffee828..0000000 --- a/Redis/redis.go +++ /dev/null @@ -1,13 +0,0 @@ -package Redis - -import ( - "github.com/go-redis/redis/v8" -) - -func GetRedis() *redis.Client { - rdb := redis.NewClient(&redis.Options{ - Addr: "127.0.0.1:6379", - DB: 0, - }) - return rdb -} diff --git a/cmd/client/main.go b/cmd/client/main.go index 929e5f8..66db6c7 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -3,6 +3,7 @@ package main import ( "chat/message" "chat/process" + "chat/user" "fmt" "net" ) @@ -29,24 +30,52 @@ func login(conn net.Conn) error { } func showMenu(name string) { - fmt.Printf("-----------------------------欢迎%s登录---------------------------\n", name) - 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 请选择(1-4):") - var k int - _, err := fmt.Scanf("%d", &k) + + for { + fmt.Printf("-----------------------------欢迎%s登录---------------------------\n", name) + 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 请选择(1-4):") + var k int + _, err := fmt.Scanf("%d", &k) + if err != nil { + fmt.Println(err) + continue + } + switch k { + case 1: + fmt.Println("在线用户列表") + case 2: + fmt.Println("发送信息") + case 4: + return + } + } + +} + +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) if err != nil { fmt.Println(err) return } - switch k { - case 1: - fmt.Println("在线用户列表") - case 2: - fmt.Println("发送信息") - } + process.WriteConn(conn, message.Message{ + Type: "add_user", + Code: 0, + Msg: "", + Data: user.User{ + Id: 0, + Name: name, + Sex: sex, + Password: pw, + }, + }) } func handleMsg() { //处理 @@ -58,6 +87,7 @@ func handleMsg() { //处理 if r, ok := c.Msg.Data.(*message.Correspond); ok { if r.Error == "" { fmt.Println("登录成功!") + showMenu(r.Msg) } else { fmt.Println("登录失败", r.Error) } @@ -65,14 +95,20 @@ func handleMsg() { //处理 fmt.Println("登录失败") } cc <- 1 + case "add_user_response": + if c.Msg.Code == 1 { + fmt.Println("添加用户成功") + } else { + fmt.Println(c.Msg.Msg) + } } } } } func main() { - conn, err := net.Dial("tcp", "127.0.0.1:8989") + if err != nil { fmt.Println(err) return @@ -89,6 +125,7 @@ func main() { fmt.Printf("\t\t\t 3.退出系统\n") fmt.Printf("\t\t\t 请选择(1-3):") _, err := fmt.Scanf("%d", &i) + fmt.Println() if err != nil { return } @@ -101,7 +138,7 @@ func main() { } <-cc case 2: - + addUser(conn) case 3: //s.Store() loop = false diff --git a/dao/dao.go b/dao/dao.go new file mode 100644 index 0000000..c4ca439 --- /dev/null +++ b/dao/dao.go @@ -0,0 +1,41 @@ +package dao + +import ( + "chat/rdm" + "chat/user" + "chat/utils" + "context" + "errors" + "strconv" +) + +var Ctx context.Context + +func init() { + Ctx = context.Background() +} + +func AddUser(user user.User) error { + getRdm := rdm.GetRdm() + if user.Name == "" { + return errors.New("user name can't be empty") + } + if user.Password == "" { + return errors.New("user password can't be empty") + } + + if utils.IsContain(user.Sex, []int8{1, 2}) < 0 { + return errors.New("user name can't be empty") + } + + r := getRdm.HMSet(Ctx, "go_chat_Users", map[string]interface{}{ + "id": strconv.Itoa(user.Id), + "name": user.Name, + "pw": user.Password, + "sex": strconv.Itoa(int(user.Sex)), + }) + if e := r.Err(); e != nil { + return e + } + return nil +} diff --git a/lib/server.go b/lib/server.go index 6f46fc8..c7babf6 100644 --- a/lib/server.go +++ b/lib/server.go @@ -1,6 +1,7 @@ package lib import ( + "chat/dao" "chat/message" "chat/process" "chat/user" @@ -34,13 +35,13 @@ func NewServer() *Server { return &Server{} } -func (server *Server) Login(uid int, pw string) error { +func (server *Server) Login(uid int, pw string) (*user.User, error) { for _, u := range server.Users { if uid == u.Id && pw == u.Password { - return nil + return &u, nil } } - return errors.New("not found this u") + return nil, errors.New("not found this u") } func (server *Server) Store() { @@ -56,12 +57,17 @@ func (server *Server) Store() { } } -func (server *Server) AddUser(name, pw string, sex int8) { +func (server *Server) AddUser(name, pw string, sex int8) error { server.Id++ u := user.User{ - server.Id, name, sex, pw, + Id: server.Id, Name: name, Sex: sex, Password: pw, } server.Users = append(server.Users, u) + err := dao.AddUser(u) + if err != nil { + return err + } + return nil } func (server *Server) Process(conn net.Conn) { @@ -74,23 +80,24 @@ func (server *Server) Process(conn net.Conn) { } func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) { - err := server.Login(s.Uid, s.Pw) + user, err := server.Login(s.Uid, s.Pw) + re := message.Message{ Type: "login_response", Data: nil, } - re.Data = message.Correspond{ + + r := message.Correspond{ Code: 1, - Msg: "success", + Msg: user.Name, Error: "", } if err != nil { - re.Data = message.Correspond{ - Code: 0, - Msg: "login fail", - Error: "pw fail or user not exist", - } + r.Code = 0 + r.Msg = "login fail" + r.Error = "pw fail or user not exist" } + re.Data = r err = process.WriteConn(conn, re) return "login_send", err @@ -107,6 +114,24 @@ func (s *Server) processConn() { if err != nil { fmt.Println(err) } + case "add_user": + u := c.Msg.Data.(*user.User) + err := s.AddUser(u.Name, u.Password, u.Sex) + r := message.Message{ + Type: "add_user_response", + Code: 1, + Msg: "", + Data: nil, + } + if err != nil { + fmt.Println(err) + r.Msg = err.Error() + r.Code = 0 + } + err = process.WriteConn(c.Conn, r) + if err != nil { + fmt.Println(err) + } } } } diff --git a/message/message.go b/message/message.go index 111ea21..656bdc6 100644 --- a/message/message.go +++ b/message/message.go @@ -1,17 +1,21 @@ package message import ( + "chat/user" "reflect" ) var MsgType = map[string]reflect.Type{ "login_send": reflect.TypeOf(&LoginS{}).Elem(), "login_response": reflect.TypeOf(&Correspond{}).Elem(), + "add_user": reflect.TypeOf(&user.User{}).Elem(), } type Message struct { //Id int Type string + Code int + Msg string Data interface{} } diff --git a/process/process.go b/process/process.go index 651dba2..59c1a98 100644 --- a/process/process.go +++ b/process/process.go @@ -49,12 +49,14 @@ func (t *Transfer) ReadConn(c chan Ms) { if err != nil { fmt.Println(err) } - tt := message.MsgType[msg.Type] - mm := message.Message{} - mm.Data = reflect.New(tt).Interface() - err = json.Unmarshal(info[:i], &mm) - if err != nil { - fmt.Println(err) + tt, ok := message.MsgType[msg.Type] + var mm = msg + if ok { + mm.Data = reflect.New(tt).Interface() + err = json.Unmarshal(info[:i], &mm) + if err != nil { + fmt.Println(err) + } } w := Ms{ Msg: mm, diff --git a/rdm/rdm.go b/rdm/rdm.go new file mode 100644 index 0000000..139751d --- /dev/null +++ b/rdm/rdm.go @@ -0,0 +1,15 @@ +package rdm + +import "github.com/go-redis/redis/v8" + +var RDM *redis.Client + +func GetRdm() *redis.Client { + if RDM == nil { + RDM = redis.NewClient(&redis.Options{ + Addr: "127.0.0.1:6379", + DB: 0, + }) + } + return RDM +} diff --git a/utils/utils.go b/utils/utils.go index d4b585b..74971fb 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -1 +1,17 @@ package utils + +import "reflect" + +func IsContain(elem interface{}, arr interface{}) int { + r := reflect.ValueOf(arr) + k := r.Kind() + if k == reflect.Slice || k == reflect.Array { + l := r.Len() + for i := 0; i < l; i++ { + if r.Index(i).Interface() == elem { + return i + } + } + } + return -1 +}