Good repo

This commit is contained in:
giongto35 2019-04-05 23:37:20 +08:00
parent 721826c9b6
commit 1398a7d3ab
2 changed files with 87 additions and 48 deletions

View file

@ -26,9 +26,36 @@ textarea {
</div>
<script>
// miscs
let log = msg => {
document.getElementById('div').innerHTML += msg + '<br>'
console.log(msg);
}
//
let pc = new RTCPeerConnection({iceServers: [{urls: 'stun:stun.l.google.com:19302'}]})
var localSessionDescription = ""
var remoteSessionDescription = ""
var conn = new WebSocket(`ws://${location.host}/ws`);
conn.onopen = () => {
log("WebSocket is opened");
}
conn.onerror = error => {
log(`Websocket error: ${error}`);
}
conn.onmessage = e => {
console.log(e.data);
// e.data in json
}
function postSession(session) {
if (session == "") {
return;
@ -46,48 +73,23 @@ function postSession(session) {
}
let pc = new RTCPeerConnection({
iceServers: [
{
urls: 'stun:stun.l.google.com:19302'
}
]
})
let log = msg => {
document.getElementById('div').innerHTML += msg + '<br>'
}
const dataChannelOptions = {
ordered: false, // do not guarantee order
maxPacketLifeTime: 3000, // in milliseconds
};
// input channel
let inputChannel = pc.createDataChannel('foo')
inputChannel.onclose = () => console.log('inputChannel has closed')
inputChannel.onopen = () => console.log('inputChannel has opened')
inputChannel.onmessage = e => log(`Message from DataChannel '${inputChannel.label}' payload '${e.data}'`)
pc.ontrack = function (event) {
var el = document.createElement(event.track.kind)
el.srcObject = event.streams[0]
el.autoplay = true
el.controls = true
document.getElementById('remoteVideos').appendChild(el)
inputChannel.onclose = () => {
log('inputChannel has closed');
}
pc.onicecandidate = event => {
if (event.candidate === null) {
var session = btoa(JSON.stringify(pc.localDescription));
localSessionDescription = session;
postSession(session)
}
inputChannel.onopen = () => {
log('inputChannel has opened');
}
pc.createOffer({offerToReceiveVideo: true, offerToReceiveAudio: true}).then(d => pc.setLocalDescription(d)).catch(log)
inputChannel.onmessage = e => {
log(`Message from DataChannel '${inputChannel.label}' payload '${e.data}'`);
}
// Input handler
keyState = {
// controllers
a: false,
@ -136,20 +138,6 @@ document.body.onkeyup = function(e){
setState(e, false);
};
window.startSession = () => {
let sd = remoteSessionDescription
if (sd === '') {
return alert('Session Description must not be empty')
}
try {
pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(atob(sd))));
} catch (e) {
alert(e);
}
}
var timer = null;
function sendInput() {
// prepare key
@ -204,5 +192,53 @@ pc.oniceconnectionstatechange = e => {
}
// stream channel
pc.ontrack = function (event) {
var el = document.createElement(event.track.kind)
el.srcObject = event.streams[0]
el.autoplay = true
el.controls = true
document.getElementById('remoteVideos').appendChild(el)
}
// candidate packet from STUN
pc.onicecandidate = event => {
if (event.candidate === null) {
// var session = btoa(JSON.stringify(pc.localDescription));
// localSessionDescription = session;
// postSession(session)
}
}
// create SDP
pc.createOffer({offerToReceiveVideo: true, offerToReceiveAudio: true}).then(d => {
pc.setLocalDescription(d, () => {
// send to ws
session = btoa(JSON.stringify(pc.localDescription));
localSessionDescription = session;
conn.send(JSON.stringify({"id": "sdp", "data": session}));
});
}).catch(log);
window.startSession = () => {
let sd = remoteSessionDescription
if (sd === '') {
return alert('Session Description must not be empty')
}
try {
pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(atob(sd))));
} catch (e) {
alert(e);
}
}
</script>
</html>

View file

@ -75,12 +75,15 @@ func ws(w http.ResponseWriter, r *http.Request) {
}
defer c.Close()
log.Println("new connection")
webRTC := webrtc.NewWebRTC()
localSession, err := webRTC.StartClient(width, height)
if err != nil {
log.Fatalln(err)
}
log.Println("new connection2")
// streaming game
// imageChannel := make(chan *image.RGBA, 100)
// go screenshotLoop(imageChannel, webRTC)