add candidate to server

This commit is contained in:
trichimtrich 2019-04-06 03:15:43 +08:00
parent 69617658af
commit 81444a405b
3 changed files with 20 additions and 1 deletions

View file

@ -62,6 +62,7 @@ conn.onmessage = e => {
startWebRTC();
break;
case "sdp":
log("Got remote sdp");
pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(atob(d["data"]))));
break;
}
@ -206,7 +207,8 @@ function endInput() {
}
pc.oniceconnectionstatechange = e => {
log(pc.iceConnectionState);
log(`iceConnectionState: ${pc.iceConnectionState}`);
if (pc.iceConnectionState === "connected") {
conn.send(JSON.stringify({"id": "start", "data": ""}));
startInput();
@ -239,6 +241,7 @@ pc.onicecandidate = event => {
// postSession(session)
} else {
console.log(JSON.stringify(event.candidate));
conn.send(JSON.stringify({"id": "candidate", "data": JSON.stringify(event.candidate)}));
}
}
@ -250,6 +253,7 @@ function startWebRTC() {
// send to ws
session = btoa(JSON.stringify(pc.localDescription));
localSessionDescription = session;
log("Send SDP to remote peer");
conn.send(JSON.stringify({"id": "sdp", "data": session}));
});

View file

@ -1,6 +1,7 @@
package main
import (
pionRTC "github.com/pions/webrtc"
"fmt"
"image"
"io/ioutil"
@ -112,6 +113,13 @@ func ws(w http.ResponseWriter, r *http.Request) {
res.Data = localSession
case "candidate":
hi := pionRTC.ICECandidateInit{}
err = json.Unmarshal([]byte(req.Data), &hi)
if err != nil {
fmt.Println("[!] Cannot parse candidate: ", err)
} else {
webRTC.AddCandidate(hi)
}
res.ID = "candidate"
case "start":

View file

@ -208,6 +208,13 @@ func (w *WebRTC) StartClient(remoteSession string, width, height int) (string, e
return localSession, nil
}
func (w *WebRTC) AddCandidate(candidate webrtc.ICECandidateInit) {
err := w.connection.AddICECandidate(candidate)
if err != nil {
fmt.Println("Cannot add candidate: ", err)
}
}
// StopClient disconnect
func (w *WebRTC) StopClient() {
fmt.Println("===StopClient===")