diff --git a/lib/LasTres/Controller/Websocket.pm b/lib/LasTres/Controller/Websocket.pm index 7c20b5a..cbd68d2 100644 --- a/lib/LasTres/Controller/Websocket.pm +++ b/lib/LasTres/Controller/Websocket.pm @@ -14,7 +14,7 @@ use Data::Dumper; use LasTres::Redis; use LasTres::Controller::Websocket::InputPackets; -my %sessions; +our %SESSIONS; use LasTres::DAO::PJs; my $result_set_pjs = LasTres::DAO::PJs->ResultSet; @@ -32,12 +32,12 @@ sub ws ($self) { ); } my $session_uuid = create_uuid_string; - $sessions{$session_uuid} = { + $SESSIONS{$session_uuid} = { user => $user, controller => $self, uuid => $session_uuid, }; - my $session = $sessions{$session_uuid}; + my $session = $SESSIONS{$session_uuid}; $self->on( json => sub ( $self, $hash ) { $self->_handle_packet( $session, $hash ); @@ -45,7 +45,12 @@ sub ws ($self) { ); $self->on( finish => sub ( $self, $code, $reason ) { - delete $sessions{$session_uuid}; + if (defined $session->{redis} && defined $session->{pj}) { + my $redis = $session->{redis}; + my $pj = $session->{pj}; + $redis->pubsub->unlisten($redis->pj_subscription($pj)); + } + delete $SESSIONS{$session_uuid}; $reason ||= "No reason"; say STDERR "Websocket for user @{[$user->username]} closed with status $code and reason $reason."; diff --git a/lib/LasTres/Controller/Websocket/InputPacket/Init.pm b/lib/LasTres/Controller/Websocket/InputPacket/Init.pm index 02d1f9f..e6073c5 100644 --- a/lib/LasTres/Controller/Websocket/InputPacket/Init.pm +++ b/lib/LasTres/Controller/Websocket/InputPacket/Init.pm @@ -14,7 +14,6 @@ use Moo; use JSON qw/to_json from_json/; use LasTres::Flags; -use LasTres::Redis; use LasTres::DAO::PJs; with 'LasTres::Controller::Websocket::InputPacket'; @@ -83,9 +82,7 @@ sub handle ( $self, $ws, $session, $data ) { clear => $JSON::true, ); $info_packet_to_send->send($ws); - require LasTres::Redis; my $redis = LasTres::Redis->new; - say $redis->pj_subscription($pj); $redis->subscribe($redis->pj_subscription($pj), my $save = sub($message, $topic, $topics) { return $self->_on_redis_event($ws, $session, $message, $topic, $topics); }); diff --git a/lib/LasTres/Controller/Websocket/InputPacket/MoveBetweenLocations.pm b/lib/LasTres/Controller/Websocket/InputPacket/MoveBetweenLocations.pm index 4b4118e..4e30cfa 100644 --- a/lib/LasTres/Controller/Websocket/InputPacket/MoveBetweenLocations.pm +++ b/lib/LasTres/Controller/Websocket/InputPacket/MoveBetweenLocations.pm @@ -64,7 +64,7 @@ sub handle ( $self, $ws, $session, $data ) { my $info_packet_to_send = LasTres::Controller::Websocket::OutputPacket::Info->new( location_data => { - current => $location->hash, + current => $team->location->hash, connected_places => $connected_places, }, );