From 2d3daa2ade3b3fdf7447e5901c22a70834dc183f Mon Sep 17 00:00:00 2001 From: giongto35 Date: Thu, 30 May 2019 22:39:52 +0800 Subject: [PATCH] Update turn by location (#46) * Config STUN by server * Move ice timeout log under condition * STUN TURN defaut when give empty string * Update git ignore to ignore prod file * Update readme --- .gitignore | 1 + README.md | 2 +- config/config.go | 3 +++ overlord/handlers.go | 26 +++++++++++++++++++++----- static/game.html | 1 + static/js/global.js | 1 + static/js/ws.js | 20 ++++++++------------ 7 files changed, 36 insertions(+), 18 deletions(-) diff --git a/.gitignore b/.gitignore index 13b6f2e7..b0acb72d 100644 --- a/.gitignore +++ b/.gitignore @@ -48,4 +48,5 @@ Temporary Items ### Production DockerfileProd key.json +run_prod_docker.sh prod/ diff --git a/README.md b/README.md index ffe05bb5..cc0c8b1d 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Klog is an open source Cloud Gaming Service building on [WebRTC](https://github. Klog aims to bring the most convenient gaming experience to gamer. You can play any games on your browser directly, which is fully compatible on multi-platform like Desktop, Android, IOS. This flexibility enables modern online gaming experience to retro games starting with NES in this current release. -Note: The current state of POGO are not optimized for production. The service will still experience lag under heavy traffic. You can try hosting your own service following the instruction in the next session. +Note: The current state of Klog are not optimized for production. The service will still experience lag under heavy traffic. You can try hosting your own service following the instruction in the next session. ![screenshot](document/img/landing-page.gif) diff --git a/config/config.go b/config/config.go index 8550dcd0..41c1285b 100644 --- a/config/config.go +++ b/config/config.go @@ -6,11 +6,14 @@ import ( ) const defaultoverlord = "ws://localhost:9000/wso" +const DefaultSTUNTURN = `[{"urls":"stun:stun-turn.webgame2d.com:3478"},{"urls":"turn:stun-turn.webgame2d.com:3478","username":"root","credential":"root"}]` var IsDebug = flag.Bool("debug", false, "Is game running in debug mode?") var OverlordHost = flag.String("overlordhost", defaultoverlord, "Specify the path for overlord. If the flag is `overlord`, the server will be run as overlord") var Port = flag.String("port", "8000", "Port of the game") var IsMonitor = flag.Bool("monitor", false, "Turn on monitor") +var FrontendSTUNTURN = flag.String("stunturn", DefaultSTUNTURN, "Frontend STUN TURN servers") + var Width = 256 var Height = 240 var WSWait = 20 * time.Second diff --git a/overlord/handlers.go b/overlord/handlers.go index a1497a4b..ccf6553c 100644 --- a/overlord/handlers.go +++ b/overlord/handlers.go @@ -3,11 +3,12 @@ package overlord import ( "errors" "fmt" - "io/ioutil" + "html/template" "log" "math/rand" "net/http" + "github.com/giongto35/cloud-game/config" "github.com/giongto35/cloud-game/cws" "github.com/giongto35/cloud-game/overlord/gamelist" "github.com/gofrs/uuid" @@ -16,7 +17,6 @@ import ( const ( gameboyIndex = "./static/game.html" - debugIndex = "./static/game.html" gamePath = "games" ) @@ -38,15 +38,31 @@ func NewServer() *Server { } } +type RenderData struct { + STUNTURN string +} + // GetWeb returns web frontend func (o *Server) GetWeb(w http.ResponseWriter, r *http.Request) { - indexFN := gameboyIndex + stunturn := *config.FrontendSTUNTURN + if stunturn == "" { + stunturn = config.DefaultSTUNTURN + } + data := RenderData{ + STUNTURN: stunturn, + } - bs, err := ioutil.ReadFile(indexFN) + tmpl, err := template.ParseFiles(gameboyIndex) if err != nil { log.Fatal(err) } - w.Write(bs) + + //bs, err := ioutil.ReadFile(indexFN) + //if err != nil { + //log.Fatal(err) + //} + //w.Write(bs) + tmpl.Execute(w, data) } // WSO handles all connections from a new worker to overlord diff --git a/static/game.html b/static/game.html index 6a0fc1c0..59a24c1c 100644 --- a/static/game.html +++ b/static/game.html @@ -74,6 +74,7 @@ diff --git a/static/js/global.js b/static/js/global.js index c2ed838b..f2ce2309 100644 --- a/static/js/global.js +++ b/static/js/global.js @@ -61,3 +61,4 @@ let isLayoutSwitched = false; var gameReady = false; var iceSuccess = true; var iceSent = false; // TODO: set to false in some init event +var defaultICE = [{urls: "stun:stun.l.google.com:19302"}] diff --git a/static/js/ws.js b/static/js/ws.js index 08372301..1b1f7d45 100644 --- a/static/js/ws.js +++ b/static/js/ws.js @@ -96,17 +96,13 @@ function sendPing() { function startWebRTC() { // webrtc - pc = new RTCPeerConnection({iceServers: [{ - urls: 'stun:stun-turn.webgame2d.com:3478' - }, - { - urls: 'stun:159.65.141.209:3478' - }, - { - urls: "turn:stun-turn.webgame2d.com:3478", - username: "root", - credential: "root" - }]}) + var iceservers = []; + if (STUNTURN == "") { + iceservers = defaultICE + } else { + iceservers = JSON.parse(STUNTURN); + } + pc = new RTCPeerConnection({iceServers: iceservers }); // input channel, ordered + reliable, id 0 inputChannel = pc.createDataChannel('a', { @@ -221,8 +217,8 @@ function startWebRTC() { // TODO: tidy up, setTimeout multiple time now // timeout setTimeout(() => { - log("Ice gathering timeout, send anyway") if (!iceSent) { + log("Ice gathering timeout, send anyway") session = btoa(JSON.stringify(pc.localDescription)); conn.send(JSON.stringify({"id": "initwebrtc", "data": session, "packet_id": curPacketID})); iceSent = true;