IM-System版本0.9.5完成私聊模式

This commit is contained in:
wandoubaba 2024-11-27 16:45:49 +08:00
parent e9376ebd2e
commit 5ab7b03a72

View File

@ -53,10 +53,55 @@ func (client *Client) menu() bool {
}
}
// 查询当前在线用户
func (client *Client) SelectUsers() {
sendMsg := "who\n"
_, err := client.conn.Write([]byte(sendMsg))
if err != nil {
fmt.Println("conn Write err:", err)
return
}
}
// 私聊模式处理方法
func (client *Client) PrivateChat() {
var remoteName string
var chatMsg string
// 查询当前在线用户名
client.SelectUsers()
fmt.Println(">>>>>> 请输入对方用户名, exit返回菜单")
fmt.Scanln(&remoteName)
for remoteName != "exit" {
fmt.Println(">>>>>> 请输入消息内容exit重新选择对象")
fmt.Scanln(&chatMsg)
for chatMsg != "exit" {
// 消息不为空则发送消息
if len(chatMsg) != 0 {
sendMsg := "to|" + remoteName + "|" + chatMsg + "\n\n"
_, err := client.conn.Write([]byte(sendMsg))
// 判断消息不为空则发送
if err != nil {
fmt.Println("conn Write err:", err)
break
}
}
// 清空上次输入
chatMsg = ""
fmt.Scanln(&chatMsg)
}
client.SelectUsers()
fmt.Println(">>>>>> 请输入对方用户名, exit返回菜单")
fmt.Scanln(&remoteName)
}
}
// 公聊模式处理方法
func (client *Client) PublicChat() {
// 提示用户输入消息
var chatMsg string
fmt.Println(">>>>>> 1请输入聊天内容exit返回菜单")
fmt.Println(">>>>>> 请输入聊内容exit返回菜单")
fmt.Scanln(&chatMsg)
// 发送给服务器
for chatMsg != "exit" {
@ -90,6 +135,7 @@ func (client *Client) UpdateName() bool {
return true
}
// 启动客户端服务
func (client *Client) Run() {
for client.flag != 0 {
for !client.menu() {
@ -102,7 +148,7 @@ func (client *Client) Run() {
client.PublicChat()
case 2:
// 私聊模式
fmt.Println("已选择私聊模式...")
client.PrivateChat()
case 3:
// 更新用户名
client.UpdateName()