From ce1502d59c074bf96cb1b36b8ade7034270ae45c Mon Sep 17 00:00:00 2001 From: wandoubaba Date: Wed, 27 Nov 2024 15:26:18 +0800 Subject: [PATCH] =?UTF-8?q?IM-System=E7=89=88=E6=9C=AC0.9.3=E5=AE=9E?= =?UTF-8?q?=E7=8E=B0client=E6=9B=B4=E6=96=B0=E7=94=A8=E6=88=B7=E5=90=8D?= =?UTF-8?q?=E5=91=BD=E4=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bilibili/aceld/IM-System/client/client.go | 25 +++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/bilibili/aceld/IM-System/client/client.go b/bilibili/aceld/IM-System/client/client.go index 39fb145..3dbf638 100644 --- a/bilibili/aceld/IM-System/client/client.go +++ b/bilibili/aceld/IM-System/client/client.go @@ -4,7 +4,9 @@ package main import ( "flag" "fmt" + "io" "net" + "os" ) type Client struct { @@ -51,6 +53,19 @@ func (client *Client) menu() bool { } } +// client的更新用户名接口 +func (client *Client) UpdateName() bool { + fmt.Println(">>>>>> 请输入新用户名:") + fmt.Scanln(&client.Name) + sendMsg := "rename|" + client.Name + "\n" + _, err := client.conn.Write([]byte(sendMsg)) + if err != nil { + fmt.Println("conn.Write err:", err) + return false + } + return true +} + func (client *Client) Run() { for client.flag != 0 { for !client.menu() { @@ -67,10 +82,17 @@ func (client *Client) Run() { case 3: // 更新用户名 fmt.Println("已选择更新用户名模式...") + client.UpdateName() } } } +// 处理server回应的消息(直接显示到标准输出) +func (client *Client) DealResponse() { + // 一旦client.conn有数据就直接copy到stdout标准输出上,永久阻塞监听 + io.Copy(os.Stdout, client.conn) +} + // 定义全局变量 var serverIp string @@ -95,6 +117,9 @@ func main() { } fmt.Println(">>>>>> 连接服务器成功……") + // 单独开启一个goroutine去处理server发送的消息 + go client.DealResponse() + // for { // time.Sleep(1 * time.Second) // }