online user list

This commit is contained in:
xing 2021-04-28 16:16:18 +08:00
parent a34e4e3629
commit d92719e20e
6 changed files with 204 additions and 35 deletions

View File

@ -1,35 +1,74 @@
package main package main
import ( import (
"bufio"
"chat/message" "chat/message"
"chat/process" "chat/process"
"chat/user" "chat/user"
"fmt" "fmt"
"net" "net"
"os"
) )
var cc = make(chan int) var menu1 = make(chan int)
var menu2 = make(chan int)
var rc = make(chan process.Ms, 20) var rc = make(chan process.Ms, 20)
var thisUser user.User
func login(conn net.Conn) error { func login(conn net.Conn) error {
var id int var id int
var pw string var pw string
_, err := fmt.Scanf("%d %s", &id, &pw) for {
stdin := bufio.NewReader(os.Stdin)
_, err := fmt.Fscanf(stdin, "%d %s", &id, &pw)
if err != nil { if err != nil {
return err fmt.Println(err, "请重新输入")
} else {
break
}
} }
var msg = message.LoginS{ var msg = message.LoginS{
Uid: id, Pw: pw, Uid: id, Pw: pw,
} }
m := message.Message{Type: "login_send", Data: msg} m := message.Message{Type: "login_send", Data: msg}
err = process.WriteConn(conn, m) err := process.WriteConn(conn, m)
if err != nil { if err != nil {
return err return err
} }
return nil return nil
} }
func showMenu(name string) { func sendMessage(conn net.Conn) {
for {
fmt.Print("请输入内容: ")
var i string
_, err := fmt.Scanf("%s", &i)
if err != nil {
fmt.Println(err)
return
}
if i == "exit" {
break
}
err = process.WriteConn(conn, message.Message{
Type: "user_message",
Code: 1,
Msg: "",
Data: message.UserMessage{
TotUid: 0,
FromUid: thisUser.Id,
FromUserName: thisUser.Name,
Msg: i,
},
})
if err != nil {
fmt.Println(err)
//return
}
}
}
func showMenu(name string, ms process.Ms) {
for { for {
fmt.Printf("-----------------------------欢迎%s登录---------------------------\n", name) fmt.Printf("-----------------------------欢迎%s登录---------------------------\n", name)
@ -47,9 +86,20 @@ func showMenu(name string) {
switch k { switch k {
case 1: case 1:
fmt.Println("在线用户列表") fmt.Println("在线用户列表")
err := process.WriteConn(ms.Conn, message.Message{
Type: "online_users",
Code: 0,
Msg: "",
Data: nil,
})
if err != nil {
fmt.Println(err)
}
<-menu2
case 2: case 2:
fmt.Println("发送信息") sendMessage(ms.Conn)
case 4: case 4:
menu1 <- 1
return return
} }
} }
@ -65,7 +115,7 @@ func addUser(conn net.Conn) {
fmt.Println(err) fmt.Println(err)
return return
} }
process.WriteConn(conn, message.Message{ err = process.WriteConn(conn, message.Message{
Type: "add_user", Type: "add_user",
Code: 0, Code: 0,
Msg: "", Msg: "",
@ -76,6 +126,10 @@ func addUser(conn net.Conn) {
Password: pw, Password: pw,
}, },
}) })
if err != nil {
fmt.Println(err)
return
}
} }
func handleMsg() { //处理 func handleMsg() { //处理
@ -87,21 +141,32 @@ func handleMsg() { //处理
if r, ok := c.Msg.Data.(*message.Correspond); ok { if r, ok := c.Msg.Data.(*message.Correspond); ok {
if r.Error == "" { if r.Error == "" {
fmt.Println("登录成功!") fmt.Println("登录成功!")
showMenu(r.Msg) thisUser = r.User
go showMenu(r.Msg, c)
} else { } else {
fmt.Println("登录失败", r.Error) fmt.Println("登录失败", r.Error)
menu1 <- 1
} }
} else { } else {
fmt.Println("登录失败") fmt.Println("登录失败")
} }
cc <- 1
case "add_user_response": case "add_user_response":
if c.Msg.Code == 1 { if c.Msg.Code == 1 {
fmt.Println("添加用户成功") fmt.Println("添加用户成功")
} else { } else {
fmt.Println(c.Msg.Msg) fmt.Println(c.Msg.Msg)
} }
cc <- 1 menu1 <- 1
case "user_message":
m := c.Msg.Data.(*message.UserMessage)
fmt.Printf("\r%s: %s\n", m.FromUserName, m.Msg)
case "online_users":
list := c.Msg.Data.(*message.UsersPres)
fmt.Printf("%s\t%s\n", "id", "昵称")
for _, pre := range list.Data {
fmt.Printf("%d\t%s\n", pre.Id, pre.Name)
}
menu2 <- 1
} }
} }
} }
@ -137,10 +202,10 @@ func main() {
if err != nil { if err != nil {
fmt.Println("login fail :", err) fmt.Println("login fail :", err)
} }
<-cc <-menu1
case 2: case 2:
addUser(conn) addUser(conn)
<-cc <-menu1
case 3: case 3:
//s.Store() //s.Store()
loop = false loop = false

View File

@ -15,24 +15,24 @@ func init() {
Ctx = context.Background() Ctx = context.Background()
} }
func AddUser(user user.User) error { func AddUser(u user.User) error {
getRdm := rdm.GetRdm() getRdm := rdm.GetRdm()
if user.Name == "" { if u.Name == "" {
return errors.New("user name can't be empty") return errors.New("user name can't be empty")
} }
if user.Password == "" { if u.Password == "" {
return errors.New("user password can't be empty") return errors.New("user password can't be empty")
} }
if utils.IsContain(user.Sex, []int8{1, 2}) < 0 { if utils.IsContain(u.Sex, []int8{1, 2}) < 0 {
return errors.New("user name can't be empty") return errors.New("user name can't be empty")
} }
r := getRdm.HMSet(Ctx, "go_chat_Users:"+strconv.Itoa(user.Id), map[string]interface{}{ r := getRdm.HMSet(Ctx, user.GetUserKey(u.Id), map[string]interface{}{
"id": strconv.Itoa(user.Id), "id": strconv.Itoa(u.Id),
"name": user.Name, "name": u.Name,
"password": user.Password, "password": u.Password,
"sex": strconv.Itoa(int(user.Sex)), "sex": strconv.Itoa(int(u.Sex)),
}) })
if e := r.Err(); e != nil { if e := r.Err(); e != nil {
return e return e

View File

@ -44,18 +44,20 @@ func (server *Server) Login(uid int, pw string) (*user.User, error) {
return &u, nil return &u, nil
} }
} }
u := rdm.GetRdm().HGetAll(context.Background(), "go_chat_Users:"+strconv.Itoa(uid)) u := rdm.GetRdm().HGetAll(context.Background(), user.GetUserKey(uid))
if u != nil && u.Val()["password"] == pw { if u != nil && u.Val()["password"] == pw {
t := u.Val() t := u.Val()
tt, _ := json.Marshal(t) id, _ := strconv.Atoi(t["id"])
uu := &user.User{} sex, _ := strconv.Atoi(t["sex"])
x := json.Unmarshal(tt, uu) uu := &user.User{
if nil != x { Id: id,
return nil, x Name: t["name"],
Sex: int8(sex),
//Password: "",
} }
return uu, nil return uu, nil
} }
return nil, errors.New("not found this u") return nil, errors.New("not found this user")
} }
func (server *Server) Store() { func (server *Server) Store() {
@ -95,7 +97,6 @@ func (server *Server) Process(conn net.Conn) {
func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) { func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) {
login, err := server.Login(s.Uid, s.Pw) login, err := server.Login(s.Uid, s.Pw)
re := message.Message{ re := message.Message{
Type: "login_response", Type: "login_response",
Data: nil, Data: nil,
@ -110,6 +111,13 @@ func (server *Server) do(s *message.LoginS, conn net.Conn) (string, error) {
r.Code = 1 r.Code = 1
r.Msg = login.Name r.Msg = login.Name
r.Error = "" r.Error = ""
r.User = *login
process.Push(&process.UserProcess{
Uid: login.Id,
Conn: conn,
})
} else {
r.Error += err.Error()
} }
re.Data = r re.Data = r
@ -146,6 +154,32 @@ func (s *Server) processConn() {
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
} }
case "user_message":
data := c.Msg.Data.(*message.UserMessage)
if data.TotUid > 0 {
} else {
fmt.Println(data.FromUserName, ":", data.Msg)
}
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)
}
} }
} }
} }
@ -155,6 +189,7 @@ func (server *Server) read(conn net.Conn) {
defer func(conn net.Conn) { defer func(conn net.Conn) {
err := conn.Close() err := conn.Close()
if err != nil { if err != nil {
process.Disconnect(conn)
fmt.Println(err, "ssssssssssssssss") fmt.Println(err, "ssssssssssssssss")
} }
}(conn) }(conn)

View File

@ -9,14 +9,32 @@ var MsgType = map[string]reflect.Type{
"login_send": reflect.TypeOf(&LoginS{}).Elem(), "login_send": reflect.TypeOf(&LoginS{}).Elem(),
"login_response": reflect.TypeOf(&Correspond{}).Elem(), "login_response": reflect.TypeOf(&Correspond{}).Elem(),
"add_user": reflect.TypeOf(&user.User{}).Elem(), "add_user": reflect.TypeOf(&user.User{}).Elem(),
"user_message": reflect.TypeOf(&UserMessage{}).Elem(),
"online_users": reflect.TypeOf(&UsersPres{}).Elem(),
} }
type Message struct { type Message struct {
//Id int //Id int
Type string Type string `json:"type"`
Code int Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data"`
}
type UsersPres struct {
Data []UserPre
}
type UserPre struct {
Id int `json:"id"`
Name string `json:"name"`
}
type UserMessage struct {
FromUid int
FromUserName string
TotUid int
Msg string Msg string
Data interface{}
} }
type LoginS struct { type LoginS struct {
@ -29,4 +47,5 @@ type Correspond struct {
Code int Code int
Msg string Msg string
Error string Error string
User user.User
} }

41
process/userManager.go Normal file
View File

@ -0,0 +1,41 @@
package process
import (
"net"
)
type UserProcess struct {
Uid int
Conn net.Conn
}
var onlineUsers = make(map[int]*UserProcess)
func GetOnlineUsers() map[int]*UserProcess {
return onlineUsers
}
func Push(process *UserProcess) {
onlineUsers[process.Uid] = process
}
func Get(id int) *UserProcess {
u, ok := onlineUsers[id]
if ok {
return u
}
return nil
}
func Del(id int) {
delete(onlineUsers, id)
}
func Disconnect(conn net.Conn) {
for u, process := range GetOnlineUsers() {
if conn.RemoteAddr() == process.Conn.RemoteAddr() {
delete(onlineUsers, u)
break
}
}
}

View File

@ -1,6 +1,11 @@
package user package user
import "errors" import (
"errors"
"fmt"
)
const Key = "go_chat_Users:%d"
type User struct { type User struct {
Id int `json:"id"` Id int `json:"id"`
@ -9,6 +14,10 @@ type User struct {
Password string `json:"password"` Password string `json:"password"`
} }
func GetUserKey(id int) string {
return fmt.Sprintf(Key, id)
}
func (u *User) CheckPassword(p string) error { func (u *User) CheckPassword(p string) error {
if p != u.Password { if p != u.Password {
return errors.New("password error") return errors.New("password error")