Maintain connection

This commit is contained in:
giongto35 2019-04-13 03:03:27 +08:00
parent 8301040779
commit 58f3d244e2
2 changed files with 13 additions and 1 deletions

View file

@ -99,4 +99,5 @@ KEY_BIT = ["a", "b", "select", "start", "up", "down", "left", "right"];
INPUT_FPS = 100;
PINGPONGPS = 5;
INPUT_STATE_PACKET = 5;

View file

@ -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