From b717b1b5eaac3672efa518509b3d3871e84900b9 Mon Sep 17 00:00:00 2001 From: Alex Karle Date: Fri, 10 Apr 2020 22:28:16 -0400 Subject: [PATCH] Euchre::Dealer: Add ping/pong heartbeat msg It seems one waay to have a keepalive for Websockets is with a heartbeat (ping/pong). Given that we don't expect millions of users at a time ;) this seems totally reasonable to fire off every 5-10 seconds to keep the connection alive. --- lib/Euchre/Dealer.pm | 6 ++++++ public/debug.html | 6 +++++- run.sh | 3 --- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm index 146f783..03da972 100644 --- a/lib/Euchre/Dealer.pm +++ b/lib/Euchre/Dealer.pm @@ -102,6 +102,7 @@ sub handle_msg { # assertions (like, needs to be their turn) my %dispatch = ( # Game management endpoints + ping => [\&pong], join_game => [\&join_game], take_seat => [\&take_seat, 'lobby', "Can't change seats during game"], stand_up => [\&stand_up, 'lobby', "Can't change seats during game"], @@ -128,6 +129,11 @@ sub handle_msg { } } +sub pong { + my ($p) = @_; + $p->{ws}->send({ json => { msg_type => 'pong' } }); +} + # player_name # game_id sub join_game { diff --git a/public/debug.html b/public/debug.html index 2764cb0..2783612 100644 --- a/public/debug.html +++ b/public/debug.html @@ -15,7 +15,7 @@ var ws = new WebSocket('ws://localhost:3000/play'); ws.onmessage = function (event) { msg = JSON.parse(event.data); - console.log(msg) + if (msg.msg_type !== 'pong') { console.log(msg) } if (msg.msg_type === 'game_state') { gameState = '
Game: ' + msg.game.id + '
' + @@ -51,6 +51,8 @@ var c = msg.hand[i] document.getElementById('hand').innerHTML += '' } + //} else if (msg.msg_type === 'pong') { + // console.log('pong!'); } }; @@ -79,6 +81,8 @@ function play(card) { ws.send(JSON.stringify({action:'play_card', card: card})) } + + window.setInterval(() => { ws.send(JSON.stringify({action:'ping'})) }, 5000);

Hello, World

diff --git a/run.sh b/run.sh index 2980278..8feaed2 100755 --- a/run.sh +++ b/run.sh @@ -19,7 +19,4 @@ depends mojo DIR=`dirname $0` cd $DIR -# XXX: this should be handled by the server... -export MOJO_INACTIVITY_TIMEOUT=1000 - exec mojo webpack ./gloat.pl -- libgit2 1.1.1