func startServer1() { serve := func(ws *websocket.Conn) { fmt.Printf("agent come") agent := fw.NewAgent(&model{}, fw.NewWsReadWriter(ws), 1) agent.Serve() } http.Handle("/agent", websocket.Handler(serve)) // http.ListenAndServe(":8000", nil) server := httptest.NewServer(nil) serverAddr = server.Listener.Addr().String() log.Info("Test WebSocket server listening on ", serverAddr) }
func main() { server, err := agent.NewServer() if err != nil { panic("new server failed") } defer func() { server.Close() }() serve := func(ws *websocket.Conn) { if err := server.Serve(fw.NewWsReadWriter(ws)); err != nil { log.Error(err.Error()) } log.Infof("agent leaves, agent count=%v", server.AgentCount()) } http.Handle("/", websocket.Handler(serve)) log.Debug("agent server start on:", cfg.AgentPort) http.ListenAndServe(":"+cfg.AgentPort, nil) }
func startServer2() { server, err := NewServer() if err != nil { log.Error(err.Error()) return } defer func() { server.Close() }() serve := func(ws *websocket.Conn) { log.Debug("new agent") if err := server.Serve(fw.NewWsReadWriter(ws)); err != nil { log.Error(err.Error()) } log.Debug("agent leave") } http.Handle("/", websocket.Handler(serve)) http.ListenAndServe(cfg.AgentAddr(), nil) log.Info("server2 serve on ", cfg.AgentAddr()) }
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) }