euchre-live

Euchre web-app for the socially distant family
git clone git://git.alexkarle.com/euchre-live.git
Log | Files | Refs | README | LICENSE

commit b717b1b5eaac3672efa518509b3d3871e84900b9 (patch)
parent ddfe9202518fdb3248eb80b8f985e9dea0268040
Author: Alex Karle <alex@karle.co>
Date:   Fri, 10 Apr 2020 22:28:16 -0400

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.

Diffstat:
Mlib/Euchre/Dealer.pm | 6++++++
Mpublic/debug.html | 6+++++-
Mrun.sh | 3---
3 files changed, 11 insertions(+), 4 deletions(-)

diff --git 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 @@ -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 = '<br>Game: ' + msg.game.id + '<br>' + @@ -51,6 +51,8 @@ var c = msg.hand[i] document.getElementById('hand').innerHTML += '<img onclick="play(' + "'" + c + "'" + ')" class="card" src="cards/' + c + '.svg">' } + //} 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); </script> <h1>Hello, World</h1> diff --git 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