go-chat/lib/server.go

314 lines
6.3 KiB
Go
Raw Normal View History

2021-04-22 06:12:35 +00:00
package lib
import (
2021-04-25 01:53:49 +00:00
"chat/dao"
2021-04-22 06:12:35 +00:00
"chat/message"
"chat/process"
2021-04-25 08:29:30 +00:00
"chat/rdm"
2021-04-22 06:12:35 +00:00
"chat/user"
2021-04-25 08:29:30 +00:00
"context"
2021-04-22 06:12:35 +00:00
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
2021-04-25 08:29:30 +00:00
"strconv"
2021-04-29 08:27:19 +00:00
"time"
2021-04-22 06:12:35 +00:00
)
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{}
}
2021-04-25 01:53:49 +00:00
func (server *Server) Login(uid int, pw string) (*user.User, error) {
2021-04-22 06:12:35 +00:00
for _, u := range server.Users {
if uid == u.Id && pw == u.Password {
2021-04-25 01:53:49 +00:00
return &u, nil
2021-04-22 06:12:35 +00:00
}
}
2021-04-28 08:16:18 +00:00
u := rdm.GetRdm().HGetAll(context.Background(), user.GetUserKey(uid))
2021-04-25 08:29:30 +00:00
if u != nil && u.Val()["password"] == pw {
t := u.Val()
2021-04-28 08:16:18 +00:00
id, _ := strconv.Atoi(t["id"])
sex, _ := strconv.Atoi(t["sex"])
uu := &user.User{
Id: id,
Name: t["name"],
Sex: int8(sex),
//Password: "",
2021-04-25 08:29:30 +00:00
}
return uu, nil
}
2021-04-28 08:16:18 +00:00
return nil, errors.New("not found this user")
2021-04-22 06:12:35 +00:00
}
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
}
}
2021-04-25 01:53:49 +00:00
func (server *Server) AddUser(name, pw string, sex int8) error {
2021-04-22 06:12:35 +00:00
server.Id++
u := user.User{
2021-04-25 01:53:49 +00:00
Id: server.Id, Name: name, Sex: sex, Password: pw,
2021-04-22 06:12:35 +00:00
}
server.Users = append(server.Users, u)
2021-04-25 01:53:49 +00:00
err := dao.AddUser(u)
if err != nil {
return err
}
return nil
2021-04-22 06:12:35 +00:00
}
func (server *Server) Process(conn net.Conn) {
m := make([]byte, 0)
read, err := conn.Read(m)
if err != nil {
return
}
fmt.Println(read)
}
2021-04-28 09:29:38 +00:00
func notice(message2 message.Message) {
for _, userProcess := range process.GetOnlineUsers() {
err := process.WriteConn(userProcess.Conn, message2)
if err != nil {
fmt.Println(err)
}
}
}
2021-04-22 06:12:35 +00:00
func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) {
2021-04-25 08:29:30 +00:00
login, err := server.Login(s.Uid, s.Pw)
2021-04-22 06:12:35 +00:00
re := message.Message{
Type: "login_response",
Data: nil,
}
2021-04-25 01:53:49 +00:00
r := message.Correspond{
2021-04-25 08:29:30 +00:00
Code: 0,
Msg: "login fail",
Error: "pw fail or login not exist ",
2021-04-22 06:12:35 +00:00
}
2021-04-25 08:29:30 +00:00
if err == nil {
r.Code = 1
r.Msg = login.Name
r.Error = ""
2021-04-28 08:16:18 +00:00
r.User = *login
2021-04-28 09:29:38 +00:00
notice(message.Message{
Type: "notice",
Code: 0,
Msg: fmt.Sprintf("%s已上线", login.Name),
Data: nil,
})
2021-04-28 08:16:18 +00:00
process.Push(&process.UserProcess{
Uid: login.Id,
Conn: conn,
})
} else {
r.Error += err.Error()
2021-04-22 06:12:35 +00:00
}
2021-04-25 01:53:49 +00:00
re.Data = r
2021-04-22 06:12:35 +00:00
err = process.WriteConn(conn, re)
return "login_send", err
}
func (s *Server) processConn() {
2021-04-29 08:27:19 +00:00
defer func() {
if r := recover(); r != nil {
fmt.Println(r)
}
}()
2021-04-22 06:12:35 +00:00
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)
}
2021-04-25 01:53:49 +00:00
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)
}
2021-04-28 08:16:18 +00:00
case "user_message":
data := c.Msg.Data.(*message.UserMessage)
2021-05-07 10:08:46 +00:00
now := time.Now().Format("2006-01-02 15:04:05")
2021-04-28 08:16:18 +00:00
if data.TotUid > 0 {
2021-04-29 08:27:19 +00:00
to := process.Get(data.TotUid)
if to == nil {
to = process.Get(data.FromUid)
if to == nil {
break
}
2021-05-07 10:08:46 +00:00
toU := dao.UserInfo(data.TotUid)
2021-04-29 08:27:19 +00:00
err := process.WriteConn(to.Conn, message.Message{
2021-05-07 10:08:46 +00:00
Type: "notice",
2021-04-29 08:27:19 +00:00
Code: 0,
2021-05-07 10:08:46 +00:00
Msg: "用户" + toU.Name + "已下线",
Data: nil,
2021-04-29 08:27:19 +00:00
})
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,
2021-05-07 10:08:46 +00:00
Datetime: now,
2021-04-29 08:27:19 +00:00
},
})
if err != nil {
fmt.Println(err)
}
2021-04-28 08:16:18 +00:00
} else {
fmt.Println(data.FromUserName, ":", data.Msg)
2021-05-07 10:08:46 +00:00
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)
}
}
2021-04-28 08:16:18 +00:00
}
case "online_users":
all := process.GetOnlineUsers()
arr := make([]message.UserPre, 0)
for _, userProcess := range all {
v := rdm.GetRdm().HGet(context.Background(), user.GetUserKey(userProcess.Uid), "name").Val()
arr = append(arr, message.UserPre{
Id: userProcess.Uid,
Name: v,
})
}
err := process.WriteConn(c.Conn, message.Message{
Type: "online_users",
Code: 1,
Msg: "",
Data: message.UsersPres{Data: arr},
})
if err != nil {
fmt.Println(err)
}
2021-04-28 09:29:38 +00:00
case "offline":
id := process.Disconnect(c.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)
}
}
2021-04-22 06:12:35 +00:00
}
}
}
}
func (server *Server) read(conn net.Conn) {
defer func(conn net.Conn) {
err := conn.Close()
if err != nil {
2021-05-07 10:08:46 +00:00
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)
}
}
2021-04-22 06:12:35 +00:00
fmt.Println(err, "ssssssssssssssss")
}
}(conn)
process.Read(conn, Rchan)
}
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)
}
}