From 94ac0f34b852e1ddcacc4558551fef2be9559188 Mon Sep 17 00:00:00 2001 From: xing Date: Thu, 22 Apr 2021 14:12:35 +0800 Subject: [PATCH] init --- Redis/redis.go | 13 +++ cmd/client/main.go | 115 ++++++++++++++++++++++++++ cmd/server/main.go | 7 ++ go.mod | 10 +++ lib/server.go | 201 +++++++++++++++++++++++++++++++++++++++++++++ message/message.go | 28 +++++++ process/process.go | 73 ++++++++++++++++ test/aa.go | 15 ++++ user/user.go | 17 ++++ utils/utils.go | 1 + 10 files changed, 480 insertions(+) create mode 100644 Redis/redis.go create mode 100644 cmd/client/main.go create mode 100644 cmd/server/main.go create mode 100644 go.mod create mode 100644 lib/server.go create mode 100644 message/message.go create mode 100644 process/process.go create mode 100644 test/aa.go create mode 100644 user/user.go create mode 100644 utils/utils.go diff --git a/Redis/redis.go b/Redis/redis.go new file mode 100644 index 0000000..ffee828 --- /dev/null +++ b/Redis/redis.go @@ -0,0 +1,13 @@ +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 new file mode 100644 index 0000000..929e5f8 --- /dev/null +++ b/cmd/client/main.go @@ -0,0 +1,115 @@ +package main + +import ( + "chat/message" + "chat/process" + "fmt" + "net" +) + +var cc = make(chan int) +var rc = make(chan process.Ms, 20) + +func login(conn net.Conn) error { + var id int + var pw string + _, err := fmt.Scanf("%d %s", &id, &pw) + if err != nil { + return err + } + var msg = message.LoginS{ + Uid: id, Pw: pw, + } + m := message.Message{Type: "login_send", Data: msg} + err = process.WriteConn(conn, m) + if err != nil { + return err + } + return nil +} + +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) + if err != nil { + fmt.Println(err) + return + } + switch k { + case 1: + fmt.Println("在线用户列表") + case 2: + fmt.Println("发送信息") + } +} + +func handleMsg() { //处理 + for { + select { + case c := <-rc: + switch c.Msg.Type { + case "login_response": + if r, ok := c.Msg.Data.(*message.Correspond); ok { + if r.Error == "" { + fmt.Println("登录成功!") + } else { + fmt.Println("登录失败", r.Error) + } + } else { + fmt.Println("登录失败") + } + cc <- 1 + } + } + } +} + +func main() { + + conn, err := net.Dial("tcp", "127.0.0.1:8989") + if err != nil { + fmt.Println(err) + return + } + go handleMsg() + go process.Read(conn, rc) + var i int + var loop = true + + for { + fmt.Println("-----------------------------欢迎登录---------------------------") + 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 请选择(1-3):") + _, err := fmt.Scanf("%d", &i) + if err != nil { + return + } + switch i { + case 1: + fmt.Print("请输入用户id和密码,空格隔开:") + err = login(conn) + if err != nil { + fmt.Println("login fail :", err) + } + <-cc + case 2: + + case 3: + //s.Store() + loop = false + default: + fmt.Println("输入有误,请重新输入") + } + if !loop { + return + } + } +} diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 0000000..77ebe23 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,7 @@ +package main + +import "chat/lib" + +func main() { + lib.NewServer().Start() +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..53da8bb --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module chat + +go 1.23 + +//require github.com/go-redis/redis/v8 v8.8.0 +require ( + github.com/gin-gonic/gin v1.7.1 + github.com/go-redis/redis/v8 v8.8.0 +//github.com/gin-gonic/gin +) diff --git a/lib/server.go b/lib/server.go new file mode 100644 index 0000000..6f46fc8 --- /dev/null +++ b/lib/server.go @@ -0,0 +1,201 @@ +package lib + +import ( + "chat/message" + "chat/process" + "chat/user" + "encoding/json" + "errors" + "fmt" + "io/ioutil" + "net" +) + +type Server struct { + Id int + Users []user.User + Messages message.Message +} + +const SavePath = "./data.json" + +var Rchan = make(chan process.Ms, 100000) + +func NewServer() *Server { + data, err := ioutil.ReadFile(SavePath) + if err == nil { + s := &Server{} + x := json.Unmarshal(data, s) + if x != nil { + return &Server{} + } + return s + } + return &Server{} +} + +func (server *Server) Login(uid int, pw string) error { + for _, u := range server.Users { + if uid == u.Id && pw == u.Password { + return nil + } + } + return errors.New("not found this u") +} + +func (server *Server) Store() { + data, err := json.Marshal(server) + if err != nil { + fmt.Println(err) + return + } + err = ioutil.WriteFile(SavePath, data, 0755) + if err != nil { + fmt.Println(err) + return + } +} + +func (server *Server) AddUser(name, pw string, sex int8) { + server.Id++ + u := user.User{ + server.Id, name, sex, pw, + } + server.Users = append(server.Users, u) +} + +func (server *Server) Process(conn net.Conn) { + m := make([]byte, 0) + read, err := conn.Read(m) + if err != nil { + return + } + fmt.Println(read) +} + +func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) { + err := server.Login(s.Uid, s.Pw) + re := message.Message{ + Type: "login_response", + Data: nil, + } + re.Data = message.Correspond{ + Code: 1, + Msg: "success", + Error: "", + } + if err != nil { + re.Data = message.Correspond{ + Code: 0, + Msg: "login fail", + Error: "pw fail or user not exist", + } + } + + err = process.WriteConn(conn, re) + return "login_send", err +} + +func (s *Server) processConn() { + for { + select { + case c := <-Rchan: + switch c.Msg.Type { + case "login_send": + l := c.Msg.Data.(*message.LoginS) + _, err := s.do(l, c.Conn) + if err != nil { + fmt.Println(err) + } + } + } + } +} + +func (server *Server) read(conn net.Conn) { + defer func(conn net.Conn) { + err := conn.Close() + if err != nil { + fmt.Println(err, "ssssssssssssssss") + } + }(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() { + listen, err := net.Listen("tcp", "0.0.0.0:8989") + if err != nil { + fmt.Println(err) + return + } + go server.processConn() + for { + conn, err := listen.Accept() + if err != nil { + fmt.Println(err) + continue + } + go server.read(conn) + } +} diff --git a/message/message.go b/message/message.go new file mode 100644 index 0000000..111ea21 --- /dev/null +++ b/message/message.go @@ -0,0 +1,28 @@ +package message + +import ( + "reflect" +) + +var MsgType = map[string]reflect.Type{ + "login_send": reflect.TypeOf(&LoginS{}).Elem(), + "login_response": reflect.TypeOf(&Correspond{}).Elem(), +} + +type Message struct { + //Id int + Type string + Data interface{} +} + +type LoginS struct { + Uid int + Pw string + Name string +} + +type Correspond struct { + Code int + Msg string + Error string +} diff --git a/process/process.go b/process/process.go new file mode 100644 index 0000000..651dba2 --- /dev/null +++ b/process/process.go @@ -0,0 +1,73 @@ +package process + +import ( + "chat/message" + "encoding/json" + "fmt" + "net" + "reflect" +) + +type Ms struct { + Msg message.Message + Conn net.Conn +} + +type Transfer struct { + Conn net.Conn + Buf [64 << 10]byte +} + +func WriteConn(c net.Conn, message message.Message) error { + m, _ := json.Marshal(message) + _, err := c.Write(m) + if err != nil { + return err + } + return nil +} + +func (t *Transfer) ReadConn(c chan Ms) { + + defer func() { + err := t.Conn.Close() + if err != nil { + fmt.Println(err) + return + } + }() + + for { + var info = t.Buf + i, err := t.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) + } + 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) + } + w := Ms{ + Msg: mm, + Conn: t.Conn, + } + c <- w + } +} + +func Read(conn net.Conn, c chan Ms) { + t := Transfer{ + Conn: conn, + Buf: [65536]byte{}, + } + t.ReadConn(c) +} diff --git a/test/aa.go b/test/aa.go new file mode 100644 index 0000000..3d98b32 --- /dev/null +++ b/test/aa.go @@ -0,0 +1,15 @@ +package main + +import "github.com/gin-gonic/gin" + +func main() { + + r := gin.Default() + r.GET("/aa", func(context *gin.Context) { + context.JSON(200, gin.H{ + "message": "pong林蝇是", + }) + }) + r.Run("0.0.0.0:9888") + +} diff --git a/user/user.go b/user/user.go new file mode 100644 index 0000000..796aaf8 --- /dev/null +++ b/user/user.go @@ -0,0 +1,17 @@ +package user + +import "errors" + +type User struct { + Id int + Name string + Sex int8 + Password string +} + +func (u *User) CheckPassword(p string) error { + if p != u.Password { + return errors.New("password error") + } + return nil +} diff --git a/utils/utils.go b/utils/utils.go new file mode 100644 index 0000000..d4b585b --- /dev/null +++ b/utils/utils.go @@ -0,0 +1 @@ +package utils