func main() { log.SetPrefix(SERVICE) lis, err := net.Listen("tcp", _port) if err != nil { log.Critical(err) os.Exit(-1) } log.Info("listenin on ", lis.Addr()) s := grpc.NewServer() ins := &server{} ins.init() pb.RegisterChatServiceServer(s, ins) s.Serve(lis) }
//游戏入口 func main() { defer utils.PrintPanicStack() go func() { log.Info(http.ListenAndServe("0.0.0.0:6060", nil)) }() log.SetPrefix(SERVICE) tcpAddr, err := net.ResolveTCPAddr("tcp4", _port) checkError(err) listener, err := net.ListenTCP("tcp", tcpAddr) checkError(err) log.Info("listening on:", listener.Addr()) // loop accepting for { conn, err := listener.AcceptTCP() if err != nil { log.Warning("accept failed:", err) continue } go handleClient(conn) // check server close signal select { case <-die: listener.Close() goto FINAL default: } } FINAL: // server closed, wait forever // 此处为什么要等1秒 // 此处等待1秒钟,只是为了wg.Wait() 可以执行到,从而阻塞主线程 for { <-time.After(time.Second) } }