Fix icecandidate setup (#44)

* Use own stun server

* Add ice candidate

* Add turn server

* Up main_test

* Fix datachannel negotiated
This commit is contained in:
giongto35 2019-05-26 22:18:41 +08:00 committed by GitHub
parent 4bf3e37fa2
commit e0a6ce2c6c
5 changed files with 49 additions and 20 deletions

View file

@ -23,7 +23,9 @@ var host = "http://localhost:8000"
// Test is in cmd, so gamePath is in parent path
var testGamePath = "../games"
var webrtcconfig = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}}
var webrtcconfig = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{
URLs: []string{"stun:stun.l.google.com:19302"},
}}}
func initOverlord() (*httptest.Server, *httptest.Server) {
server := overlord.NewServer()

View file

@ -13,8 +13,6 @@ type BrowserClient struct {
// RouteBrowser are all routes server accepts for browser
func (s *Session) RouteBrowser() {
iceCandidates := [][]byte{}
browserClient := s.BrowserClient
browserClient.Receive("heartbeat", func(resp cws.WSPacket) cws.WSPacket {
@ -22,9 +20,15 @@ func (s *Session) RouteBrowser() {
})
browserClient.Receive("icecandidate", func(resp cws.WSPacket) cws.WSPacket {
log.Println("Received candidates ", resp.Data)
log.Println("Overlord: Received icecandidate from a browser", resp.Data)
log.Println("Overlord: Relay icecandidate from a browser to worker")
wc, ok := s.handler.workerClients[s.ServerID]
if !ok {
return cws.EmptyPacket
}
wc.Send(resp, nil)
iceCandidates = append(iceCandidates, []byte(resp.Data))
return cws.EmptyPacket
})

16
static/js/ws.js vendored
View file

@ -96,7 +96,15 @@ function sendPing() {
function startWebRTC() {
// webrtc
pc = new RTCPeerConnection({iceServers: [{urls: ['stun:stun.l.google.com:19302']}]})
pc = new RTCPeerConnection({iceServers: [{
urls: 'stun:159.65.141.209:3478',
username: "root",
credential: "root"
}, {
urls: "turn:159.65.141.209:3478",
username: "root",
credential: "root"
}]})
// input channel, ordered + reliable, id 0
inputChannel = pc.createDataChannel('a', {
@ -173,11 +181,15 @@ function startWebRTC() {
if (pc.iceConnectionState === "connected") {
gameReady = true
iceSuccess = true
//conn.send(JSON.stringify({"id": "start", "data": ""}));
conn.send(JSON.stringify({"id": "icecandidate", "data": e.candidate}));
}
else if (pc.iceConnectionState === "failed") {
gameReady = false
iceSuccess = false
log(`failed. Retry...`)
pc.createOffer({iceRestart: true }).then(d => {
pc.setLocalDescription(d).catch(log);
}).catch(log);
}
else if (pc.iceConnectionState === "disconnected") {
stopInputTimer();

View file

@ -16,7 +16,7 @@ import (
"github.com/pion/webrtc/v2/pkg/media"
)
var webrtcconfig = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:stun.l.google.com:19302"}}}}
var webrtcconfig = webrtc.Configuration{ICEServers: []webrtc.ICEServer{{URLs: []string{"stun:159.65.141.209:3478"}}}}
// Allows compressing offer/answer to bypass terminal input limits.
const compress = false
@ -92,7 +92,7 @@ type WebRTC struct {
}
// StartClient start webrtc
func (w *WebRTC) StartClient(remoteSession string, iceCandidates [][]byte, width, height int) (string, error) {
func (w *WebRTC) StartClient(remoteSession string, iceCandidates []string, width, height int) (string, error) {
defer func() {
if err := recover(); err != nil {
log.Println(err)
@ -146,7 +146,7 @@ func (w *WebRTC) StartClient(remoteSession string, iceCandidates [][]byte, width
// input channel
inputTrack, err := w.connection.CreateDataChannel("a", &webrtc.DataChannelInit{
Ordered: &dfalse,
Negotiated: &dfalse,
Negotiated: &dtrue,
ID: &d0,
})
@ -197,14 +197,15 @@ func (w *WebRTC) StartClient(remoteSession string, iceCandidates [][]byte, width
// Parse candidates list
// This logic is wrong
//for _, bcandidate := range iceCandidates {
//iceCandidate := webrtc.ICECandidateInit{}
//if err := json.Unmarshal(bcandidate, &iceCandidate); err != nil {
//log.Println("Cannot parse ", bcandidate)
//continue
//}
//w.connection.AddICECandidate(iceCandidate)
//}
for _, bcandidate := range iceCandidates {
iceCandidate := webrtc.ICECandidateInit{}
if err := json.Unmarshal([]byte(bcandidate), &iceCandidate); err != nil {
log.Println("Cannot parse ", bcandidate)
continue
}
log.Println("Add iceCandidate: ", iceCandidate)
w.connection.AddICECandidate(iceCandidate)
}
answer, err := w.connection.CreateAnswer(nil)
if err != nil {

View file

@ -32,7 +32,7 @@ func NewOverlordClient(oc *websocket.Conn) *OverlordClient {
// RouteOverlord are all routes server received from overlord
func (h *Handler) RouteOverlord() {
iceCandidates := [][]byte{}
iceCandidates := map[string][]string{}
oClient := h.oClient
// Received from overlord the serverID
@ -52,7 +52,7 @@ func (h *Handler) RouteOverlord() {
func(resp cws.WSPacket) (req cws.WSPacket) {
log.Println("Received relay SDP of a browser from overlord")
peerconnection := webrtc.NewWebRTC()
localSession, err := peerconnection.StartClient(resp.Data, iceCandidates, config.Width, config.Height)
localSession, err := peerconnection.StartClient(resp.Data, iceCandidates[resp.SessionID], config.Width, config.Height)
//h.peerconnections[resp.SessionID] = peerconnection
// Create new sessions when we have new peerconnection initialized
@ -160,6 +160,16 @@ func (h *Handler) RouteOverlord() {
return req
})
oClient.Receive(
"icecandidate",
func(resp cws.WSPacket) (req cws.WSPacket) {
log.Println("Received a icecandidate from overlord: ", resp.Data)
iceCandidates[resp.SessionID] = append(iceCandidates[resp.SessionID], resp.Data)
return cws.EmptyPacket
},
)
oClient.Receive(
"terminateSession",
func(resp cws.WSPacket) (req cws.WSPacket) {