From 58f3d244e2a3b1c9ef129fcd72783cdefa99c2c2 Mon Sep 17 00:00:00 2001 From: giongto35 Date: Sat, 13 Apr 2019 03:03:27 +0800 Subject: [PATCH] Maintain connection --- static/js/const.js | 1 + static/js/ws.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/static/js/const.js b/static/js/const.js index 2899c46f..11f49ae0 100644 --- a/static/js/const.js +++ b/static/js/const.js @@ -99,4 +99,5 @@ KEY_BIT = ["a", "b", "select", "start", "up", "down", "left", "right"]; INPUT_FPS = 100; +PINGPONGPS = 5; INPUT_STATE_PACKET = 5; diff --git a/static/js/ws.js b/static/js/ws.js index eedbb3b0..9b7e5c7a 100644 --- a/static/js/ws.js +++ b/static/js/ws.js @@ -18,7 +18,9 @@ function startGame() { // Clear old roomID conn.onopen = () => { log("WebSocket is opened. Send ping"); - conn.send(JSON.stringify({"id": "ping", "data": GAME_LIST[gameIdx].nes, "room_id": roomID.value, "player_index": parseInt(playerIndex.value, 10)})); + conn.send(JSON.stringify({"id": "ping", "data": GAME_LIST[gameIdx].nes, "room_id": roomID.value, "player_index": parseInt(playerIndex.value, 10)}));inputTimer + log("Send ping pong frequently") + pingpongTimer = setInterval(sendPing, 1000 / PINGPONGPS) } conn.onerror = error => { @@ -33,9 +35,13 @@ function startGame() { d = JSON.parse(e.data); switch (d["id"]) { case "pong": + // TODO: Change name use one session log("Recv pong. Start webrtc"); startWebRTC(); break; + case "pingpong": + // TODO: Calc time + break; case "sdp": log("Got remote sdp"); pc.setRemoteDescription(new RTCSessionDescription(JSON.parse(atob(d["data"])))); @@ -54,6 +60,11 @@ function startGame() { } } + function sendPing() { + // TODO: format the package with time + conn.send(JSON.stringify({"id": "pingpong", "data": "pingpong"})); + } + // webrtc pc = new RTCPeerConnection({iceServers: [{urls: 'stun:stun.l.google.com:19302'}]}) // input channel