Skip to content

Commit

Permalink
complete
Browse files Browse the repository at this point in the history
客户端功能完成开发
  • Loading branch information
kunpen committed Oct 29, 2021
1 parent 41f5d3a commit 218128e
Showing 1 changed file with 122 additions and 5 deletions.
127 changes: 122 additions & 5 deletions Client_go/Client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package main
import (
"flag"
"fmt"
"io"
"net"
"os"
)

type Clinet struct{
type Client struct{
ServerIp string
ServerPort int
Name string
Expand All @@ -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,
Expand All @@ -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.私聊模式")
Expand All @@ -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

}
Expand All @@ -100,6 +212,11 @@ func main(){
fmt.Println("connection fault\n")
return
}


//单独启动监听go程
go client.DealRepsone()

fmt.Println("conection success\n")

//启动服务端业务
Expand Down

0 comments on commit 218128e

Please sign in to comment.