From 2cb4cd41200a50296728cb9565923f12e260002f Mon Sep 17 00:00:00 2001 From: wandoubaba Date: Wed, 27 Nov 2024 15:15:47 +0800 Subject: [PATCH] =?UTF-8?q?IM-System=E7=89=88=E6=9C=AC0.9.2=E8=AE=A9client?= =?UTF-8?q?=E5=8F=AF=E4=BB=A5=E6=8E=A5=E6=94=B6=E5=91=BD=E4=BB=A4=E8=A1=8C?= =?UTF-8?q?=E5=8F=82=E6=95=B0=EF=BC=8C=E5=AE=9E=E7=8E=B0=E5=AE=A2=E6=88=B7?= =?UTF-8?q?=E7=AB=AF=E8=8F=9C=E5=8D=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bilibili/aceld/IM-System/client/client.go | 67 +++++++++++++++++++++-- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/bilibili/aceld/IM-System/client/client.go b/bilibili/aceld/IM-System/client/client.go index e3516cc..39fb145 100644 --- a/bilibili/aceld/IM-System/client/client.go +++ b/bilibili/aceld/IM-System/client/client.go @@ -2,9 +2,9 @@ package main import ( + "flag" "fmt" "net" - "time" ) type Client struct { @@ -12,6 +12,7 @@ type Client struct { ServerPort int Name string conn net.Conn + flag int // 当前client模式 } func NewClient(serverIp string, serverPort int) *Client { @@ -19,6 +20,7 @@ func NewClient(serverIp string, serverPort int) *Client { client := &Client{ ServerIp: serverIp, ServerPort: serverPort, + flag: 999, } // 连接Server conn, err := net.Dial("tcp", fmt.Sprintf("%s:%d", serverIp, serverPort)) @@ -31,15 +33,70 @@ func NewClient(serverIp string, serverPort int) *Client { return client } +func (client *Client) menu() bool { + var flag int + fmt.Println("1. 公聊模式") + fmt.Println("2. 私聊模式") + fmt.Println("3. 更新用户名") + fmt.Println("0. 退出") + + fmt.Scanln(&flag) + client.flag = flag + + if flag >= 0 && flag <= 3 { + return true + } else { + fmt.Println(">>>>>> 请输入合法的数字 <<<<<<") + return false + } +} + +func (client *Client) Run() { + for client.flag != 0 { + for !client.menu() { + + } + // 根据不同的模式处理不同的业务 + switch client.flag { + case 1: + // 公聊模式 + fmt.Println("已选择公聊模式...") + case 2: + // 私聊模式 + fmt.Println("已选择私聊模式...") + case 3: + // 更新用户名 + fmt.Println("已选择更新用户名模式...") + } + } +} + +// 定义全局变量 +var serverIp string + +// 定义全局变量 +var serverPort int + +// init函数会在main函数之前执行 +func init() { + // ./client -ip 127.0.0.1 + flag.StringVar(&serverIp, "ip", "127.0.0.1", "设置服务端IP地址(默认127.0.0.1)") + // ./client -port 8888 + flag.IntVar(&serverPort, "port", 8888, "设置服务端端口(默认8888)") +} + func main() { - client := NewClient("127.0.0.1", 8888) + // 解析命令行传参 + flag.Parse() + client := NewClient(serverIp, serverPort) if client == nil { fmt.Println(">>>>>> 连接服务器失败!") return } fmt.Println(">>>>>> 连接服务器成功……") - for { - time.Sleep(1 * time.Second) - } + // for { + // time.Sleep(1 * time.Second) + // } + client.Run() }