Jelajahi Sumber

Add active mq message

Ronald Peterson 10 tahun lalu
induk
melakukan
1372a17a95
2 mengubah file dengan 32 tambahan dan 13 penghapusan
  1. 24 9
      games/games.go
  2. 8 4
      main.go

+ 24 - 9
games/games.go

@@ -1,17 +1,22 @@
 package games
 
+import (
+	"encoding/json"
+
+	extStomp "github.com/go-stomp/stomp"
+)
+
 type Games struct {
 	addChan  chan *Game
 	listChan chan listChan
-	getChan chan getGame
+	getChan  chan getGame
 }
 
 type listChan chan []*Game
 
 type getGame struct {
-	id int64
+	id         int64
 	returnChan chan *Game
-
 }
 
 func New() Games {
@@ -19,7 +24,7 @@ func New() Games {
 	games := Games{
 		addChan:  make(chan *Game),
 		listChan: make(chan listChan),
-		getChan: make(chan getGame),
+		getChan:  make(chan getGame),
 	}
 
 	go serveGames(games)
@@ -45,8 +50,19 @@ func serveGames(games Games) {
 	}
 }
 
-func (games Games) AddGame(game *Game) {
+type stompGameStart struct {
+	Type   string `json:"type"`
+	GameId int64  `json:"gameid"`
+}
+
+func (games Games) AddGame(game *Game, stompConnection *extStomp.Conn) {
 	games.addChan <- game
+	stompGameStart := stompGameStart{
+		Type:   "GAME_START",
+		GameId: game.Id,
+	}
+	b, _ := json.Marshal(stompGameStart)
+	stompConnection.Send("gopinguin.game", "", b)
 }
 
 func (games Games) ListGames() []*Game {
@@ -56,11 +72,10 @@ func (games Games) ListGames() []*Game {
 }
 
 func (games Games) GetGame(id int64) *Game {
-	getGame := getGame {
-		id: id,
+	getGame := getGame{
+		id:         id,
 		returnChan: make(chan *Game),
-		
 	}
 	games.getChan <- getGame
-	return <- getGame.returnChan
+	return <-getGame.returnChan
 }

+ 8 - 4
main.go

@@ -8,16 +8,19 @@ import (
 	"strconv"
 
 	"github.com/julienschmidt/httprouter"
-
+	extStomp "github.com/go-stomp/stomp"
+	
 	"battlecamp-go-server/games"
 	"battlecamp-go-server/stomp"
 )
 
 var currentGames games.Games = games.New()
+var stompConnection *extStomp.Conn
+
 
 func main() {
-	conn := stomp.DailStomp()
-	defer conn.Disconnect()
+	stompConnection = stomp.DailStomp()
+	defer stompConnection.Disconnect()
 	log.Fatal(http.ListenAndServe(":8080", initRouter()))
 }
 
@@ -57,7 +60,7 @@ func createGame(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
 		game = games.NewGame(56, 35)
 	}
 
-	currentGames.AddGame(game)
+	currentGames.AddGame(game, stompConnection)
 	w.WriteHeader(http.StatusCreated)
 	e := json.NewEncoder(w)
 	e.Encode(game)
@@ -113,3 +116,4 @@ func showAsciiBoard(w http.ResponseWriter, req *http.Request, ps httprouter.Para
 	b := currentGames.GetGame(id).Board.String()
 	w.Write([]byte(b))
 }
+