Revert "Good repo"

This reverts commit a278fb4cd9.
This commit is contained in:
giongto35 2019-04-06 00:20:04 +08:00
parent 1398a7d3ab
commit 80b6e62cb8
2 changed files with 48 additions and 87 deletions

View file

@ -26,36 +26,9 @@ 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;
@ -73,23 +46,48 @@ 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 = () => {
log('inputChannel has closed');
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.onopen = () => {
log('inputChannel has opened');
pc.onicecandidate = event => {
if (event.candidate === null) {
var session = btoa(JSON.stringify(pc.localDescription));
localSessionDescription = session;
postSession(session)
}
}
inputChannel.onmessage = e => {
log(`Message from DataChannel '${inputChannel.label}' payload '${e.data}'`);
}
pc.createOffer({offerToReceiveVideo: true, offerToReceiveAudio: true}).then(d => pc.setLocalDescription(d)).catch(log)
// Input handler
keyState = {
// controllers
a: false,
@ -138,6 +136,20 @@ 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
@ -192,53 +204,5 @@ 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,15 +75,12 @@ 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)