group chat

This commit is contained in:
xing 2021-05-07 18:08:46 +08:00
parent b0680cb406
commit cfa5e13aec
5 changed files with 66 additions and 22 deletions

View File

@ -8,8 +8,6 @@ import (
"fmt" "fmt"
"net" "net"
"os" "os"
"strconv"
"strings"
) )
var menu1 = make(chan int) var menu1 = make(chan int)
@ -41,6 +39,16 @@ func login(conn net.Conn) error {
} }
func sendMessage(conn net.Conn) { func sendMessage(conn net.Conn) {
id := 0
for {
fmt.Print("请输入用户id(0为所有):")
_, err := fmt.Fscanf(bufio.NewReader(os.Stdin), "%d", &id)
if err != nil {
fmt.Println(err)
} else {
break
}
}
for { for {
fmt.Print("请输入内容: ") fmt.Print("请输入内容: ")
var i string var i string
@ -53,11 +61,7 @@ func sendMessage(conn net.Conn) {
if i == "exit" { if i == "exit" {
break break
} }
a := strings.Split(i, "|") uid := id
uid := 0
if id, ok := strconv.Atoi(a[0]); ok == nil {
uid = id
}
err = process.WriteConn(conn, message.Message{ err = process.WriteConn(conn, message.Message{
Type: "user_message", Type: "user_message",
Code: 1, Code: 1,
@ -66,7 +70,7 @@ func sendMessage(conn net.Conn) {
TotUid: uid, TotUid: uid,
FromUid: thisUser.Id, FromUid: thisUser.Id,
FromUserName: thisUser.Name, FromUserName: thisUser.Name,
Msg: a[1], Msg: i,
}, },
}) })
if err != nil { if err != nil {
@ -83,7 +87,7 @@ func showMenu(name string, ms process.Ms) {
fmt.Printf("\t\t\t 1.显示在线用户列表\n") fmt.Printf("\t\t\t 1.显示在线用户列表\n")
fmt.Printf("\t\t\t 2.发送消息\n") fmt.Printf("\t\t\t 2.发送消息\n")
fmt.Printf("\t\t\t 3.信息列表\n") fmt.Printf("\t\t\t 3.信息列表\n")
fmt.Printf("\t\t\t 4.退出\n") fmt.Printf("\t\t\t 4.返回上一级菜单\n")
fmt.Printf("\t\t\t 请选择(1-4):") fmt.Printf("\t\t\t 请选择(1-4):")
var k int var k int
_, err := fmt.Scanf("%d", &k) _, err := fmt.Scanf("%d", &k)
@ -186,6 +190,9 @@ func handleMsg() { //处理
menu2 <- 1 menu2 <- 1
case "notice": case "notice":
fmt.Printf("\n系统:%s\n", c.Msg.Msg) fmt.Printf("\n系统:%s\n", c.Msg.Msg)
case "all_users":
m := c.Msg.Data.(*message.AllUser)
fmt.Printf("\r%s(%d) %s\n%s\n", m.FromUserName, m.FromUid, m.DateTime, m.Msg)
} }
} }
} }

View File

@ -177,6 +177,7 @@ func (s *Server) processConn() {
} }
case "user_message": case "user_message":
data := c.Msg.Data.(*message.UserMessage) data := c.Msg.Data.(*message.UserMessage)
now := time.Now().Format("2006-01-02 15:04:05")
if data.TotUid > 0 { if data.TotUid > 0 {
to := process.Get(data.TotUid) to := process.Get(data.TotUid)
if to == nil { if to == nil {
@ -184,17 +185,12 @@ func (s *Server) processConn() {
if to == nil { if to == nil {
break break
} }
toU := dao.UserInfo(data.TotUid)
err := process.WriteConn(to.Conn, message.Message{ err := process.WriteConn(to.Conn, message.Message{
Type: "user_message", Type: "notice",
Code: 0, Code: 0,
Msg: "user had offline", Msg: "用户" + toU.Name + "已下线",
Data: message.UserMessage{ Data: nil,
FromUid: 0,
FromUserName: "系统",
TotUid: data.FromUid,
Msg: "user had offline",
Datetime: time.Now().Format("2006-01-02 15:04:05"),
},
}) })
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -210,7 +206,7 @@ func (s *Server) processConn() {
FromUserName: data.FromUserName, FromUserName: data.FromUserName,
TotUid: data.TotUid, TotUid: data.TotUid,
Msg: data.Msg, Msg: data.Msg,
Datetime: time.Now().Format("2006-01-02 15:04:05"), Datetime: now,
}, },
}) })
if err != nil { if err != nil {
@ -219,6 +215,22 @@ func (s *Server) processConn() {
} else { } else {
fmt.Println(data.FromUserName, ":", data.Msg) fmt.Println(data.FromUserName, ":", data.Msg)
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)
}
}
} }
case "online_users": case "online_users":
all := process.GetOnlineUsers() all := process.GetOnlineUsers()
@ -262,7 +274,19 @@ 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) 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)
}
}
fmt.Println(err, "ssssssssssssssss") fmt.Println(err, "ssssssssssssssss")
} }
}(conn) }(conn)

View File

@ -11,6 +11,7 @@ var MsgType = map[string]reflect.Type{
"add_user": reflect.TypeOf(&user.User{}).Elem(), "add_user": reflect.TypeOf(&user.User{}).Elem(),
"user_message": reflect.TypeOf(&UserMessage{}).Elem(), "user_message": reflect.TypeOf(&UserMessage{}).Elem(),
"online_users": reflect.TypeOf(&UsersPres{}).Elem(), "online_users": reflect.TypeOf(&UsersPres{}).Elem(),
"all_users": reflect.TypeOf(&AllUser{}).Elem(),
} }
type Message struct { type Message struct {
@ -21,6 +22,13 @@ type Message struct {
Data interface{} `json:"data"` Data interface{} `json:"data"`
} }
type AllUser struct {
FromUid int
FromUserName string
Msg string
DateTime string
}
type UsersPres struct { type UsersPres struct {
Data []UserPre Data []UserPre
} }

View File

@ -4,6 +4,7 @@ import (
"chat/message" "chat/message"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io"
"net" "net"
"reflect" "reflect"
) )
@ -41,7 +42,11 @@ func (t *Transfer) ReadConn(c chan Ms) {
var info = t.Buf var info = t.Buf
i, err := t.Conn.Read(info[:]) i, err := t.Conn.Read(info[:])
if err != nil { if err != nil {
fmt.Println(err, "errrrrrrrrrrrrr") if err == io.EOF {
fmt.Println("连接已断开。。。。")
} else {
fmt.Println(err, "errrrrrrrrrrrrr")
}
return return
} }
msg := message.Message{} msg := message.Message{}

View File

@ -36,7 +36,7 @@ func Disconnect(conn net.Conn) int {
for u, process := range GetOnlineUsers() { for u, process := range GetOnlineUsers() {
if conn.RemoteAddr() == process.Conn.RemoteAddr() { if conn.RemoteAddr() == process.Conn.RemoteAddr() {
fmt.Println("用户", u, "已下线") fmt.Println("用户", u, "已下线")
delete(onlineUsers, u) Del(u)
return u return u
} }
} }