Golang TCP 即时通讯实例
main.go package main func main(){ server := NewServer("127.0.0.1", 8888) server.Start() } server.go package main import( "fmt" "net" "sync" "io" "time" ) type Server struct { Ip string Port int //在线用户列表 OnlineMap map[string]*User mapLock sync.RWMutex //消息广播channel 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 } //监听Message广播消息channel的goroutine,一旦有消息就发送给全部在线的User func (this *Server) ListenMessager() { for { msg := <-this....