IM-System版本0.9.4实现公聊模式

This commit is contained in:
wandoubaba 2024-11-27 16:33:42 +08:00
parent ce1502d59c
commit e9376ebd2e

View File

@ -53,11 +53,35 @@ func (client *Client) menu() bool {
}
}
func (client *Client) PublicChat() {
// 提示用户输入消息
var chatMsg string
fmt.Println(">>>>>> 1请输入聊天内容exit返回菜单")
fmt.Scanln(&chatMsg)
// 发送给服务器
for chatMsg != "exit" {
// 消息不为空则发送消息
if len(chatMsg) != 0 {
sendMsg := chatMsg + "\n"
_, err := client.conn.Write([]byte(sendMsg))
// 判断消息不为空则发送
if err != nil {
fmt.Println("conn Write err:", err)
break
}
}
// 清空上次输入
chatMsg = ""
fmt.Scanln(&chatMsg)
}
}
// client的更新用户名接口
func (client *Client) UpdateName() bool {
fmt.Println(">>>>>> 请输入新用户名:")
fmt.Scanln(&client.Name)
sendMsg := "rename|" + client.Name + "\n"
// 把sendMsg发送给server端
_, err := client.conn.Write([]byte(sendMsg))
if err != nil {
fmt.Println("conn.Write err:", err)
@ -74,15 +98,16 @@ func (client *Client) Run() {
// 根据不同的模式处理不同的业务
switch client.flag {
case 1:
// 公聊模式
fmt.Println("已选择公聊模式...")
// 公聊模式s
client.PublicChat()
case 2:
// 私聊模式
fmt.Println("已选择私聊模式...")
case 3:
// 更新用户名
fmt.Println("已选择更新用户名模式...")
client.UpdateName()
default:
fmt.Println("无效的菜单命令")
}
}
}
@ -116,12 +141,8 @@ func main() {
return
}
fmt.Println(">>>>>> 连接服务器成功……")
// 单独开启一个goroutine去处理server发送的消息
go client.DealResponse()
// for {
// time.Sleep(1 * time.Second)
// }
// 启动客户端服务
client.Run()
}