func NewServer() (*Server, error) { // connect dao server dao: daocli, err := rpc.Dial("tcp", cfg.DaoAddr()) if err != nil { log.Warningf("dao server connect fail(err=%+v), try again...", err.Error()) time.Sleep(1 * time.Second) goto dao } ctrl := make(chan string, 10) game: cli, err := fw.NewClient(cfg.GameAddr(), &gameCliModel{}, ctrl) if err != nil { log.Error(err.Error()) log.Warningf("game server connect fail(err=%+v), try again...", err.Error()) time.Sleep(1 * time.Second) goto game } go cli.Loop() // daemon routine (to be done), with "ctrl" // ...... //////////////////// // new server serverInst = &Server{ dao: daocli, gameCli: cli, gameCtrl: ctrl, connIdAcc: 0, agents: make(map[int]*fw.Agent, 1000), } return serverInst, nil }
func main() { var agent *fw.Agent serve := func(ws *websocket.Conn) { // only ONE agent server allowed to connect log.Debugf("new comes, agent=%+v", agent) if agent == nil { agent = fw.NewAgent(game.NewModel(), fw.NewWsReadWriter(ws), 1) if err := agent.Serve(); err != nil { log.Error(err.Error()) } } else { log.Warning("already connected by agentsrv, cannont serve more") } // release agent = nil } http.Handle("/", websocket.Handler(serve)) log.Debug("game server start on:", cfg.GameAddr()) http.ListenAndServe(cfg.GameAddr(), nil) }