This commit is contained in:
xing 2021-04-22 14:12:35 +08:00
commit 94ac0f34b8
10 changed files with 480 additions and 0 deletions

13
Redis/redis.go Normal file
View File

@ -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
}

115
cmd/client/main.go Normal file
View File

@ -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
}
}
}

7
cmd/server/main.go Normal file
View File

@ -0,0 +1,7 @@
package main
import "chat/lib"
func main() {
lib.NewServer().Start()
}

10
go.mod Normal file
View File

@ -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
)

201
lib/server.go Normal file
View File

@ -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)
}
}

28
message/message.go Normal file
View File

@ -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
}

73
process/process.go Normal file
View File

@ -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)
}

15
test/aa.go Normal file
View File

@ -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")
}

17
user/user.go Normal file
View File

@ -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
}

1
utils/utils.go Normal file
View File

@ -0,0 +1 @@
package utils