区块链

go语言简单聊天室

2019-12-31  本文已影响0人  骑蜗上高速

Go语言编写简单聊天室

package main
import (
    "fmt"
    "net"
    "strings"
    "time"
)
type Client struct {
    c chan string
    Name string
    Addr string
}
//创建全局map,存储在线用户
var onlineMap map[string]Client
//创建全局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)  {
    //监听用户的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()
    //创建channel,判断用户是否活跃
    hasData := make(chan bool)
    //获取用户addr, ip+port
    addr := conn.RemoteAddr().String()
    //创建用户
    client := Client{make(chan string),addr,addr}
    //将新连接用户添加到在线用户的map中
    onlineMap[addr] = client
    //创建用来给当前用户发送消息的go程
    go WriteMsgToClient(conn,client)
    //发送用户上线消息到全局channel中
    //message <- "[" + addr + "]" + client.Name + " login"
    message <- MakeMsg(client,"login")
    //创建一个channel,判断用户退出状态
    quit := make(chan bool)
    //创建一个匿名go程,专门处理用户发送的消息
    go func() {
        buf := make([]byte, 4096)
        for {
            n, err := conn.Read(buf)
            if n == 0 {
                quit <- true
                fmt.Printf("检测到用户%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的计时器
        case <- time.After(time.Second * 10):
            delete(onlineMap,client.Addr)//将用户从列表中删除
            message <- MakeMsg(client,"logout")//写入用户退出消息到全局channel
            return
        }
    }
}
func Manager ()  {
    //初始化map
    onlineMap =  make(map[string]Client)
    //循环监听message,是否有数据,转储到msg,无数据就阻塞
    for {
        msg := <- message
        //循环发送消息给在线用户
        for _, client := range onlineMap {
            client.c <- msg
        }
    }
}
func main() {
    //创建监听
    listener, err := net.Listen("tcp","127.0.0.1:8008")
    if err != nil {
        fmt.Println("Listen err------>",err)
        return
    }
    defer listener.Close()
    //创建管理者goroutine,管理map和全局channel
    go Manager()
    //循环监听客户端连接
    for {
        conn, err := listener.Accept()
        if err != nil {
            fmt.Println("Accept err------>",err)
            return
        }
        //启动go程处理客户端
        go HandlerConn(conn)
    }
}
上一篇下一篇

猜你喜欢

热点阅读