commit ed46a8246a3d487d59a550033901ed6d9c5dc2ee (patch)
parent e3256f83c6e111c63c099c39445b393fd49fd7b3
Author: Alex Karle <alex@karle.co>
Date: Tue, 31 Mar 2020 23:54:57 -0400
Euchre::Dealer: Fix double JSON encoding
I was wondering when I whipped up the prototype why I needed to call
JSON.parses() twice...
Turns out ws->send() encodes it for us, so there's no need to use
encode_json explicitly!
Diffstat:
2 files changed, 5 insertions(+), 6 deletions(-)
diff --git a/lib/Euchre/Dealer.pm b/lib/Euchre/Dealer.pm
@@ -5,7 +5,6 @@ use warnings;
package Euchre::Dealer;
use List::Util;
-use Mojo::JSON qw(encode_json);
use Euchre::Card;
use Euchre::Game;
@@ -225,11 +224,11 @@ sub deal_players_hands {
for my $p (@{$game->{players}}) {
my @hand = map { cid_to_name($_) } @{shift @$handsA};
$p->{ws}->send({ json =>
- encode_json({
+ {
msg_type => 'deal',
hand => \@hand,
trump_nominee => $nominee,
- })
+ }
});
}
}
@@ -254,7 +253,7 @@ sub broadcast_gamestate {
spectators => \@snames,
};
- my $json = encode_json({ msg_type => 'game_state', game => $msg });
+ my $json = { msg_type => 'game_state', game => $msg };
for my $ws (@all_ws) {
$ws->send({ json => $json});
}
@@ -264,7 +263,7 @@ sub broadcast_gamestate {
sub send_error {
my ($p, $msg) = @_;
my $ws = $p->{ws};
- my $json = encode_json({ msg_type => 'error', msg => $msg });
+ my $json = { msg_type => 'error', msg => $msg };
$ws->send({ json => $json});
}
diff --git a/public/debug.html b/public/debug.html
@@ -9,7 +9,7 @@
<script>
var ws = new WebSocket('ws://localhost:3000/play');
ws.onmessage = function (event) {
- msg = JSON.parse(JSON.parse(event.data)); // double parsed?
+ msg = JSON.parse(event.data);
console.log(msg)
if (msg.msg_type === 'game_state') {