mirror of
https://github.com/giongto35/cloud-game.git
synced 2026-07-22 17:47:11 +00:00
Maintain connection
This commit is contained in:
parent
8301040779
commit
58f3d244e2
2 changed files with 13 additions and 1 deletions
|
|
@ -99,4 +99,5 @@ KEY_BIT = ["a", "b", "select", "start", "up", "down", "left", "right"];
|
|||
|
||||
|
||||
INPUT_FPS = 100;
|
||||
PINGPONGPS = 5;
|
||||
INPUT_STATE_PACKET = 5;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue