示例#1
0
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)
}
示例#2
0
func (s *Server) Serve(rwc fw.ReadWriteCloser) (err error) {
	s.connIdAcc++
	agent := fw.NewAgent(&model{dao: s.dao}, rwc, s.connIdAcc)

	// add to map
	s.mu.Lock()
	s.agents[s.connIdAcc] = agent
	s.mu.Unlock()

	defer func() {
		s.mu.Lock()
		delete(s.agents, agent.ConnId)
		s.mu.Unlock()
		defer agent.Close() // close it!
	}()

	if err = agent.Serve(); err != nil {
		return
	}
	return
}
示例#3
0
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)

}