Skip to content

Commit

Permalink
Update turn by location (#46)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
giongto35 committed May 30, 2019
1 parent a9b4e31 commit fcd30ac
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -48,4 +48,5 @@ Temporary Items
### Production
DockerfileProd
key.json
run_prod_docker.sh
prod/
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -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)

Expand Down
3 changes: 3 additions & 0 deletions config/config.go
Expand Up @@ -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
26 changes: 21 additions & 5 deletions overlord/handlers.go
Expand Up @@ -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"
Expand All @@ -16,7 +17,6 @@ import (

const (
gameboyIndex = "./static/game.html"
debugIndex = "./static/game.html"
gamePath = "games"
)

Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions static/game.html
Expand Up @@ -74,6 +74,7 @@

<script>
DEBUG = true;
STUNTURN = {{.STUNTURN}};
</script>

<script src="/static/js/jquery-3.3.1.min.js"></script>
Expand Down
1 change: 1 addition & 0 deletions static/js/global.js
Expand Up @@ -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"}]
20 changes: 8 additions & 12 deletions static/js/ws.js
Expand Up @@ -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', {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit fcd30ac

Please sign in to comment.