From e96b2a40728d7b2a05456f52f83d1e5012548bc7 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sun, 14 Apr 2019 01:21:14 +0800 Subject: [PATCH] WIP --- static/js/gameboy_controller.js | 4 +--- static/js/global.js | 3 +-- static/js/ws.js | 29 +++++++++++++++-------------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/static/js/gameboy_controller.js b/static/js/gameboy_controller.js index d87872d5..ce12dee2 100644 --- a/static/js/gameboy_controller.js +++ b/static/js/gameboy_controller.js @@ -63,7 +63,6 @@ function chooseGame(idx, force=false) { function setState(e, bo) { if (e.keyCode in KEY_MAP) { keyState[KEY_MAP[e.keyCode]] = bo; - stateUnchange = false; unchangePacket = INPUT_STATE_PACKET; } } @@ -149,7 +148,7 @@ document.body.onkeydown = function (e) { function sendInput() { // prepare key - if (stateUnchange || unchangePacket > 0) { + if (unchangePacket > 0) { st = ""; KEY_BIT.slice().reverse().forEach(elem => { st += keyState[elem] ? 1 : 0; @@ -162,7 +161,6 @@ function sendInput() { a[0] = ss; inputChannel.send(a); - stateUnchange = false; unchangePacket--; } } diff --git a/static/js/global.js b/static/js/global.js index 0bc060be..0ddec381 100644 --- a/static/js/global.js +++ b/static/js/global.js @@ -32,7 +32,6 @@ keyState = { quit: false } -stateUnchange = true; unchangePacket = INPUT_STATE_PACKET; inputTimer = null; @@ -53,4 +52,4 @@ function log(msg) { document.getElementById('div').innerHTML += msg + '
' console.log(msg); } -} \ No newline at end of file +} diff --git a/static/js/ws.js b/static/js/ws.js index 1aba878c..ee8429cd 100644 --- a/static/js/ws.js +++ b/static/js/ws.js @@ -98,6 +98,21 @@ function startWebRTC() { pc.createOffer({offerToReceiveVideo: true, offerToReceiveAudio: false}).then(d => { pc.setLocalDescription(d).catch(log); }) + + // input channel + inputChannel = pc.createDataChannel('foo') + inputChannel.onclose = () => { + log('inputChannel has closed'); + } + + inputChannel.onopen = () => { + log('inputChannel has opened'); + } + + inputChannel.onmessage = e => { + log(`Message from DataChannel '${inputChannel.label}' payload '${e.data}'`); + } + } function startGame() { @@ -114,19 +129,5 @@ function startGame() { } // end clear - // input channel - inputChannel = pc.createDataChannel('foo') - inputChannel.onclose = () => { - log('inputChannel has closed'); - } - - inputChannel.onopen = () => { - log('inputChannel has opened'); - } - - inputChannel.onmessage = e => { - log(`Message from DataChannel '${inputChannel.label}' payload '${e.data}'`); - } - startInput(); }