Refactor mode select

This commit is contained in:
giongto35 2019-05-01 15:56:31 +08:00
parent 142b9d7bb4
commit e7567c3fa3
3 changed files with 24 additions and 18 deletions

View file

@ -19,13 +19,10 @@ const (
debugIndex = "./static/index_ws.html"
)
var indexFN = gameboyIndex
// Time allowed to write a message to the peer.
var readWait = 30 * time.Second
var writeWait = 30 * time.Second
var IsOverlord = false
var upgrader = websocket.Upgrader{}
// initilizeOverlord setup an overlord server
@ -56,7 +53,7 @@ func initializeServer() {
log.Println("Run as a single server")
}
handler := handler.NewHandler(conn)
handler := handler.NewHandler(conn, *config.IsDebug)
// ignore origin
upgrader.CheckOrigin = func(r *http.Request) bool { return true }
@ -71,24 +68,16 @@ func initializeServer() {
func main() {
flag.Parse()
log.Println("Usage: ./game [debug]")
if *config.IsDebug {
// debug
indexFN = debugIndex
log.Println("Use debug version")
}
log.Println("Usage: ./game [-debug]")
if *config.OverlordHost == "overlord" {
log.Println("Running as overlord ")
initilizeOverlord()
IsOverlord = true
initilizeOverlord()
} else {
if strings.HasPrefix(*config.OverlordHost, "ws") && !strings.HasSuffix(*config.OverlordHost, "wso") {
log.Fatal("Overlord connection is invalid. Should have the form `ws://.../wso`")
}
log.Println("Running as slave ")
IsOverlord = false
initializeServer()
}
}

View file

@ -26,7 +26,7 @@ func initOverlord() *httptest.Server {
}
func initServer(t *testing.T, oconn *websocket.Conn) *httptest.Server {
handler := handler.NewHandler(oconn)
handler := handler.NewHandler(oconn, true)
server := httptest.NewServer(http.HandlerFunc(handler.WS))
return server
}

View file

@ -8,6 +8,8 @@ import (
"time"
"github.com/giongto35/cloud-game/config"
"github.com/giongto35/cloud-game/cws"
"github.com/giongto35/cloud-game/handler/gamelist"
"github.com/giongto35/cloud-game/webrtc"
"github.com/gorilla/websocket"
uuid "github.com/satori/go.uuid"
@ -20,14 +22,11 @@ const (
debugIndex = "./static/index_ws.html"
)
var indexFN = debugIndex
// Time allowed to write a message to the peer.
var readWait = 30 * time.Second
var writeWait = 30 * time.Second
// Flag to determine if the server is overlord or not
var IsOverlord = false
var upgrader = websocket.Upgrader{}
// ID to peerconnection
@ -38,6 +37,10 @@ type Handler struct {
oClient *OverlordClient
rooms map[string]*Room
serverID string
// isDebug determines the mode handler is running
isDebug bool
isOverlord bool
// ID to peerconnection
peerconnections map[string]*webrtc.WebRTC
// Session
@ -45,12 +48,13 @@ type Handler struct {
}
// NewHandler returns a new server
func NewHandler(overlordConn *websocket.Conn) *Handler {
func NewHandler(overlordConn *websocket.Conn, isDebug bool) *Handler {
//conn, err := createOverlordConnection()
//if err != nil {
//return nil, err
//}
return &Handler{
isDebug: isDebug,
oClient: NewOverlordClient(overlordConn),
rooms: map[string]*Room{},
peerconnections: map[string]*webrtc.WebRTC{},
@ -59,6 +63,13 @@ func NewHandler(overlordConn *websocket.Conn) *Handler {
// GetWeb returns web frontend
func (h *Handler) GetWeb(w http.ResponseWriter, r *http.Request) {
indexFN := ""
if h.isDebug {
indexFN = debugIndex
} else {
indexFN = gameboyIndex
}
bs, err := ioutil.ReadFile(indexFN)
if err != nil {
log.Fatal(err)
@ -92,6 +103,12 @@ func (h *Handler) WS(w http.ResponseWriter, r *http.Request) {
go wssession.OverlordClient.Heartbeat()
go wssession.OverlordClient.Listen()
}
wssession.BrowserClient.Send(cws.WSPacket{
ID: "gamelist",
Data: gamelist.GetEncodedGameList(),
}, nil)
wssession.BrowserClient.Listen()
defer c.Close()