用户上线及广播功能
作者:互联网
package main import "net" type User struct { Name string Addr string C chan string conn net.Conn } func newUser(conn net.Conn) *User { userAddr := conn.RemoteAddr().String() user := &User{ Name: userAddr, Addr: userAddr, C: make(chan string), conn: conn, } //开启goroutine go user.ListenMessage() return user } //监听当前用户channel func (this *User) ListenMessage() { for { msg := <-this.C this.conn.Write([]byte(msg + "\n")) } }
package main import ( "fmt" "net" "sync" ) type Server struct { Ip string Port int //在线用户 OnlineMap map[string]*User maplock sync.RWMutex //消息 Message chan string } func NewServer(ip string, port int) *Server { server := &Server{ Ip: ip, Port: port, OnlineMap: make(map[string]*User), Message: make(chan string), } return server } func (this *Server) ChrodCast(user *User, msg string) { sendMsg := "[" + user.Addr + "]" + user.Name + ":" + msg this.Message <- sendMsg } func (this *Server) Handler(conn net.Conn) { // fmt.Println("已连接") user := newUser(conn) this.maplock.Lock() this.OnlineMap[user.Name] = user this.maplock.Unlock() //发送消息 this.ChrodCast(user, "已上线") select {} } //监听当前channel 消息 func (this *Server) ListenMessager() { for { msg := <-this.Message this.maplock.Lock() for _, cli := range this.OnlineMap { cli.C <- msg } this.maplock.Unlock() } } func (this *Server) Start() { //监听连接 listener, err := net.Listen("tcp", fmt.Sprintf("%s:%d", this.Ip, this.Port)) if err != nil { fmt.Println("listen 监听err:", err) } defer listener.Close() go this.ListenMessager() for { //accpet conn, err := listener.Accept() if err != nil { fmt.Println("conn连接err", err) continue } //handler go this.Handler(conn) } }
标签:上线,string,chan,用户,广播,User,net,conn,user 来源: https://www.cnblogs.com/alway-learn/p/16083044.html