mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-23 10:07:30 +00:00
Stun turn from worker (#65)
* Stun turn from worker * Add Stun turn from server * Add google stun to ice list
This commit is contained in:
parent
e034de5b2c
commit
4efaf40bfa
5 changed files with 43 additions and 35 deletions
|
|
@ -15,6 +15,7 @@ var IsMonitor = flag.Bool("monitor", false, "Turn on monitor")
|
|||
var FrontendSTUNTURN = flag.String("stunturn", DefaultSTUNTURN, "Frontend STUN TURN servers")
|
||||
var Mode = flag.String("mode", "dev", "Environment")
|
||||
var IsRetro = flag.Bool("isretro", true, "Is retro")
|
||||
var StunTurnTemplate = `[{"urls":"stun:stun.l.google.com:19302"},{"urls":"stun:%s:3478"},{"urls":"turn:%s:3478","username":"root","credential":"root"}]`
|
||||
|
||||
var WSWait = 20 * time.Second
|
||||
var MatchWorkerRandom = false
|
||||
|
|
|
|||
|
|
@ -1,8 +1,6 @@
|
|||
package gamelist
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
|
@ -23,10 +21,3 @@ func GetGameList(gamePath string) []string {
|
|||
|
||||
return games
|
||||
}
|
||||
|
||||
// GetEncodedGameList returns game list in encoded wspacket format
|
||||
func GetEncodedGameList(gamePath string) string {
|
||||
encodedList, _ := json.Marshal(GetGameList(gamePath))
|
||||
fmt.Println(encodedList)
|
||||
return string(encodedList)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -91,7 +91,7 @@ func (o *Server) WSO(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
}
|
||||
client := NewWorkerClient(c, serverID, address)
|
||||
client := NewWorkerClient(c, serverID, address, fmt.Sprintf(config.StunTurnTemplate, address, address))
|
||||
o.workerClients[serverID] = client
|
||||
defer o.cleanConnection(client, serverID)
|
||||
|
||||
|
|
@ -160,8 +160,8 @@ func (o *Server) WS(w http.ResponseWriter, r *http.Request) {
|
|||
wssession.RouteBrowser()
|
||||
|
||||
wssession.BrowserClient.Send(cws.WSPacket{
|
||||
ID: "gamelist",
|
||||
Data: gamelist.GetEncodedGameList(gamePath),
|
||||
ID: "init",
|
||||
Data: createInitPackage(o.workerClients[serverID].StunTurnServer, gamePath),
|
||||
}, nil)
|
||||
|
||||
// If peerconnection is done (client.Done is signalled), we close peerconnection
|
||||
|
|
@ -272,6 +272,8 @@ func getLatencyMapFromBrowser(workerClients map[string]*WorkerClient, client *Br
|
|||
return latencyMap
|
||||
}
|
||||
|
||||
// cleanConnection is called when a worker is disconnected
|
||||
// connection from worker (client) to server is also closed
|
||||
func (o *Server) cleanConnection(client *WorkerClient, serverID string) {
|
||||
log.Println("Unregister server from overlord")
|
||||
// Remove serverID from servers
|
||||
|
|
@ -285,3 +287,12 @@ func (o *Server) cleanConnection(client *WorkerClient, serverID string) {
|
|||
|
||||
client.Close()
|
||||
}
|
||||
|
||||
// createInitPackage returns serverhost + game list in encoded wspacket format
|
||||
// This package will be sent to initialize
|
||||
func createInitPackage(stunturn, gamePath string) string {
|
||||
gameList := gamelist.GetGameList(gamePath)
|
||||
initPackage := append([]string{stunturn}, gameList...)
|
||||
encodedList, _ := json.Marshal(initPackage)
|
||||
return string(encodedList)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -9,9 +9,10 @@ import (
|
|||
|
||||
type WorkerClient struct {
|
||||
*cws.Client
|
||||
ServerID string
|
||||
Address string
|
||||
IsAvailable bool
|
||||
ServerID string
|
||||
Address string
|
||||
StunTurnServer string
|
||||
IsAvailable bool
|
||||
}
|
||||
|
||||
// RouteWorker are all routes server received from worker
|
||||
|
|
@ -42,11 +43,12 @@ func (o *Server) RouteWorker(workerClient *WorkerClient) {
|
|||
}
|
||||
|
||||
// NewWorkerClient returns a client connecting to worker. This connection exchanges information between workers and server
|
||||
func NewWorkerClient(c *websocket.Conn, serverID string, address string) *WorkerClient {
|
||||
func NewWorkerClient(c *websocket.Conn, serverID string, address string, stunturn string) *WorkerClient {
|
||||
return &WorkerClient{
|
||||
Client: cws.NewClient(c),
|
||||
ServerID: serverID,
|
||||
Address: address,
|
||||
IsAvailable: true,
|
||||
Client: cws.NewClient(c),
|
||||
ServerID: serverID,
|
||||
Address: address,
|
||||
StunTurnServer: stunturn,
|
||||
IsAvailable: true,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
33
static/js/ws.js
vendored
33
static/js/ws.js
vendored
|
|
@ -24,11 +24,18 @@ conn.onmessage = e => {
|
|||
d = JSON.parse(e.data);
|
||||
switch (d["id"]) {
|
||||
|
||||
case "gamelist":
|
||||
// parse files list to gamelist
|
||||
files = JSON.parse(d["data"]);
|
||||
case "init":
|
||||
// TODO: Read from struct
|
||||
// init package has 2 part [stunturn, gamelist]
|
||||
// The first element is stunturn address
|
||||
// The rest are list of game
|
||||
data = JSON.parse(d["data"]);
|
||||
stunturn = data[0]
|
||||
startWebRTC(stunturn);
|
||||
data.shift()
|
||||
gameList = [];
|
||||
files.forEach(file => {
|
||||
|
||||
data.forEach(file => {
|
||||
var file = file
|
||||
var name = file.substr(0, file.indexOf('.'));
|
||||
gameList.push({file: file, name: name});
|
||||
|
|
@ -102,8 +109,8 @@ conn.onmessage = e => {
|
|||
console.log(latenciesMap)
|
||||
|
||||
conn.send(JSON.stringify({"id": "checkLatency", "data": JSON.stringify(latenciesMap), "packet_id": latencyPacketID}));
|
||||
startWebRTC();
|
||||
}
|
||||
//startWebRTC();
|
||||
}
|
||||
}
|
||||
xmlHttp.onload = () => {
|
||||
cntResp++;
|
||||
|
|
@ -116,8 +123,8 @@ conn.onmessage = e => {
|
|||
|
||||
//conn.send(JSON.stringify({"id": "checkLatency", "data": latenciesMap, "packet_id": latencyPacketID}));
|
||||
conn.send(JSON.stringify({"id": "checkLatency", "data": JSON.stringify(latenciesMap), "packet_id": latencyPacketID}));
|
||||
startWebRTC();
|
||||
}
|
||||
//startWebRTC();
|
||||
}
|
||||
}
|
||||
xmlHttp.send( null );
|
||||
}
|
||||
|
|
@ -141,14 +148,10 @@ function sendPing() {
|
|||
conn.send(JSON.stringify({"id": "heartbeat", "data": Date.now().toString()}));
|
||||
}
|
||||
|
||||
function startWebRTC() {
|
||||
function startWebRTC(iceservers) {
|
||||
log(`received stunturn from worker ${iceservers}`)
|
||||
// webrtc
|
||||
var iceservers = [];
|
||||
if (STUNTURN == "") {
|
||||
iceservers = defaultICE
|
||||
} else {
|
||||
iceservers = JSON.parse(STUNTURN);
|
||||
}
|
||||
iceservers = JSON.parse(iceservers);
|
||||
pc = new RTCPeerConnection({iceServers: iceservers });
|
||||
|
||||
// input channel, ordered + reliable, id 0
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue