5.5 Go语言项目实战:多人聊天室
生活随笔
收集整理的這篇文章主要介紹了
5.5 Go语言项目实战:多人聊天室
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
功能需求
- 實(shí)現(xiàn)單撩
- 實(shí)現(xiàn)群撩
- 實(shí)現(xiàn)用戶上線的全網(wǎng)通知
- 實(shí)現(xiàn)用戶昵稱
- 實(shí)現(xiàn)聊天日志的存儲(chǔ)和查看
服務(wù)端實(shí)現(xiàn)
type Client struct {conn net.Connname stringaddr string }var (//客戶端信息,用昵稱為鍵//clientsMap = make(map[string]net.Conn)clientsMap = make(map[string]Client) )func SHandleError(err error, why string) {if err != nil {fmt.Println(why, err)os.Exit(1)} }func main() {//建立服務(wù)端監(jiān)聽listener, e := net.Listen("tcp", "127.0.0.1:8888")SHandleError(e, "net.Listen")defer func() {for _, client := range clientsMap {client.conn.Write([]byte("all:服務(wù)器進(jìn)入維護(hù)狀態(tài),大家都洗洗睡吧!"))}listener.Close()}()for {//循環(huán)接入所有女朋友conn, e := listener.Accept()SHandleError(e, "listener.Accept")clientAddr := conn.RemoteAddr()//TODO:接收并保存昵稱buffer := make([]byte, 1024)var clientName stringfor {n, err := conn.Read總結(jié)
以上是生活随笔為你收集整理的5.5 Go语言项目实战:多人聊天室的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: github搜索技巧:搜索star数量大
- 下一篇: 计算机辐射测试,无线路由器辐射测试方法