func (s *Server) checkGameHandler(w http.ResponseWriter, r *http.Request) { authToken, err := s.getToken(w, r, "auth-token") if err != nil { http.Error(w, "Invalid auth-token", http.StatusBadRequest) return } user, err := s.api.GetUser(authToken) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if user == nil { http.Error(w, "Invalid auth-token", http.StatusBadRequest) return } s.navigation.giMutex.RLock() game, ok := s.navigation.games[user.Name] s.navigation.giMutex.RUnlock() var message protocol.Message if ok { message = protocol.NewSearchGameReadyMessage(authToken, game.gtype, game.players, protocol.SuccessMessageStatus) } else { var players []string message = protocol.NewSearchGameReadyMessage(authToken, "", players, protocol.FailureMessageStatus) } data, _ := s.messageFactory.EncodeMessage(message) w.Write(data) }
func (s *Server) searchGameHandler(w http.ResponseWriter, r *http.Request) { authToken, err := s.getToken(w, r, "auth-token") if err != nil { http.Error(w, "Invalid auth-token", http.StatusBadRequest) return } user, err := s.api.GetUser(authToken) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if user == nil { http.Error(w, "Invalid auth-token", http.StatusBadRequest) return } cnotify, ok := w.(http.CloseNotifier) if !ok { http.Error(w, "cannot stream", http.StatusInternalServerError) return } cond := true gnotify := s.searchGame(user.Name, &cond) select { case <-cnotify.CloseNotify(): cond = false break case game := <-gnotify: log.Printf("game: %v", game) message := protocol.NewSearchGameReadyMessage(authToken, game.gtype, game.players, protocol.SuccessMessageStatus) data, _ := s.messageFactory.EncodeMessage(message) w.Write(data) break } cond = false log.Println("complete search game") }