Go語(yǔ)言編寫簡(jiǎn)單聊天室
package main
import (
"fmt"
"net"
"strings"
"time"
)
type Client struct {
c chan string
Name string
Addr string
}
//創(chuàng)建全局map,存儲(chǔ)在線用戶
var onlineMap map[string]Client
//創(chuàng)建全局channel,傳遞用戶消息
var message = make(chan string)
func MakeMsg(client Client,msg string) (buf string){
buf = "[" + client.Addr + "]" + client.Name + ": " + msg
return
}
func WriteMsgToClient(conn net.Conn,client Client) {
//監(jiān)聽用戶的channel是的有消息
for msg := range client.c {
_, err := conn.Write([]byte(msg + "\n"))
if err != nil {
fmt.Println("Write err------>",err)
continue
}
}
}
func HandlerConn(conn net.Conn) {
defer conn.Close()
//創(chuàng)建channel,判斷用戶是否活躍
hasData := make(chan bool)
//獲取用戶addr, ip+port
addr := conn.RemoteAddr().String()
//創(chuàng)建用戶
client := Client{make(chan string),addr,addr}
//將新連接用戶添加到在線用戶的map中
onlineMap[addr] = client
//創(chuàng)建用來(lái)給當(dāng)前用戶發(fā)送消息的go程
go WriteMsgToClient(conn,client)
//發(fā)送用戶上線消息到全局channel中
//message <- "[" + addr + "]" + client.Name + " login"
message <- MakeMsg(client,"login")
//創(chuàng)建一個(gè)channel鲜漩,判斷用戶退出狀態(tài)
quit := make(chan bool)
//創(chuàng)建一個(gè)匿名go程漾岳,專門處理用戶發(fā)送的消息
go func() {
buf := make([]byte, 4096)
for {
n, err := conn.Read(buf)
if n == 0 {
quit <- true
fmt.Printf("檢測(cè)到用戶%s退出\n",client.Name)
return
}
if err != nil {
fmt.Println("conn Read Err------>",err)
return
}
msg := string(buf[:n-1])
//獲取在線用戶列表
if msg == "who" && len(msg) == 3 {
_,_ = conn.Write([]byte("online user list:\n"))
for _,user := range onlineMap {
userInfo := user.Addr + ":" + user.Name + "\n"
_,_ = conn.Write([]byte(userInfo))
}
//判斷用戶修改用戶名命令
} else if len(msg) >= 8 && msg[:6] == "rename"{
newname := strings.Split(msg,"|")[1]
client.Name = newname //修改用戶名
onlineMap[addr] = client//更新用戶列表
conn.Write([]byte("rename successeful!\n"))
}else {
//將讀到的用戶消息廣播給所有在線用戶
message <- MakeMsg(client,msg)
}
hasData <- true
}
}()
for {
select {
case <- quit:
delete(onlineMap,client.Addr)//將用戶從列表中刪除
message <- MakeMsg(client,"logout")//寫入用戶退出消息到全局channel
return
case <- hasData:
//目的是重置下面的case的計(jì)時(shí)器
case <- time.After(time.Second * 10):
delete(onlineMap,client.Addr)//將用戶從列表中刪除
message <- MakeMsg(client,"logout")//寫入用戶退出消息到全局channel
return
}
}
}
func Manager () {
//初始化map
onlineMap = make(map[string]Client)
//循環(huán)監(jiān)聽message墓卦,是否有數(shù)據(jù)十酣,轉(zhuǎn)儲(chǔ)到msg灾杰,無(wú)數(shù)據(jù)就阻塞
for {
msg := <- message
//循環(huán)發(fā)送消息給在線用戶
for _, client := range onlineMap {
client.c <- msg
}
}
}
func main() {
//創(chuàng)建監(jiān)聽
listener, err := net.Listen("tcp","127.0.0.1:8008")
if err != nil {
fmt.Println("Listen err------>",err)
return
}
defer listener.Close()
//創(chuàng)建管理者goroutine馅闽,管理map和全局channel
go Manager()
//循環(huán)監(jiān)聽客戶端連接
for {
conn, err := listener.Accept()
if err != nil {
fmt.Println("Accept err------>",err)
return
}
//啟動(dòng)go程處理客戶端
go HandlerConn(conn)
}
}