From 218128e83fa5e76bfbc8da4225be0ffdcb533d00 Mon Sep 17 00:00:00 2001 From: kunpen <42486863+kunpen@users.noreply.github.com> Date: Fri, 29 Oct 2021 20:54:23 +0800 Subject: [PATCH] complete MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 客户端功能完成开发 --- Client_go/Client.go | 127 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 122 insertions(+), 5 deletions(-) diff --git a/Client_go/Client.go b/Client_go/Client.go index 2c37188..51c97eb 100644 --- a/Client_go/Client.go +++ b/Client_go/Client.go @@ -3,10 +3,12 @@ package main import ( "flag" "fmt" + "io" "net" + "os" ) -type Clinet struct{ +type Client struct{ ServerIp string ServerPort int Name string @@ -15,9 +17,9 @@ type Clinet struct{ } -func NewClint(serverIp string,serverPort int) *Clinet { +func NewClint(serverIp string,serverPort int) *Client { //创建客户端对象 - client := &Clinet{ + client := &Client{ ServerIp: serverIp, ServerPort: serverPort, flag: 999, @@ -38,7 +40,7 @@ func NewClint(serverIp string,serverPort int) *Clinet { } -func (client *Clinet)menu() bool { +func (client *Client)menu() bool { var flag int fmt.Println("1.群聊模式") fmt.Println("2.私聊模式") @@ -58,23 +60,133 @@ func (client *Clinet)menu() bool { } } -func (client *Clinet)Run() { +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 error",err) + return false + + } + return true + +} +func (client *Client)PublicChat() { + var chatMsg string + fmt.Println(">>请输入聊天内容,exit退出<<") + fmt.Scanln(&chatMsg) + for chatMsg !="exit"{ + //发送给服务器消息 + //不为空则发送 + if len(chatMsg)!=0{ + sendMsg := chatMsg+"\n\r" + _,err:=client.conn.Write([]byte(sendMsg)) + if err!=nil { + fmt.Println("conn write err:",err) + break + } + + } + chatMsg="" + fmt.Println(">>请输入聊天内容,exit退出<<") + fmt.Scanln(&chatMsg) + + } + +} +//查询在线用户 +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) + + if RemoteName!="exit" { + + fmt.Println("请输入消息内容.exit退出") + fmt.Scanln(&ChatMsg) + + for ChatMsg!="exit" { + if len(ChatMsg)!=0{ + sendMsg := "to|"+RemoteName+"|"+ChatMsg+"\n\r" + _,err:=client.conn.Write([]byte(sendMsg)) + if err!=nil { + fmt.Println("conn write err:",err) + break + } + + } + ChatMsg="" + fmt.Println(">>请输入聊天内容,exit退出<<") + fmt.Scanln(&ChatMsg) + + } + client.SelectUsers() + fmt.Println(">>请输入聊天对象[用户名],exit 退出<<") + fmt.Scanln(&RemoteName) + + } + + + +} + + + +//处理回应消息,显示到标准输出即可 +func (client *Client)DealRepsone() { + + //for { + // buff := make() + // client.conn.Read(buff) + // fmt.Println(buff) + //} + //等效io.copy + + + io.Copy(os.Stdout,client.conn) + //一旦io.conn有数据就拷贝到 os.stdout + //永久阻塞监听 + +} +func (client *Client)Run() { for client.flag !=0{ for client.menu()!=true{ } switch client.flag { case 1://群聊模式 fmt.Println("使用群聊模式。。。。。") + client.PublicChat() break case 2://私聊模式 fmt.Println("使用私聊模式。。。。。") + client.PrivateChat() break case 3://rename username fmt.Println("修改用户名。。。。。。") + client.UpdateName() break } @@ -100,6 +212,11 @@ func main(){ fmt.Println("connection fault\n") return } + + + //单独启动监听go程 + go client.DealRepsone() + fmt.Println("conection success\n") //启动服务端业务