jagordon59 has asked for the wisdom of the Perl Monks concerning the following question:
I have an online game with the game server written in Perl 5.32 hosted on Amazon Linux where players connect from their browser using web sockets. In addition to real players connecting, Bot players can also connect which is via a Perl script on the same server where the game server is running. The game works fine but of course you have to consider cases where a user wants to explicitly quit the game, closes their browser (or tab) or refreshes their browser tab. To deal with these I set an alarm for 1 second whenever getting a disconnect. If a player was just refreshing their browser, they will reconnect immediately cancelling the alarm and all is good. If they were explicitly quitting or closed their browser tab, there will be no reconnect and the alarm should go off and the sub will remove them from the game. This all works fine when it's just real players in a game all connected from their browser. However, when there is one or more bots connected and one of the real players disconnects, the alarm is set but NEVER goes off.
The failure occurs the next time the game server has to send a message out to all connected players. Since the alarm didn't go off, that disconnected player didn't get removed from the list. And when time the game server tries to send a message to that player, it pukes due to a syswrite error on the socket that was internally closed.
I have tried using unsafe signals and POSIX signal handler but neither of these work any differently or better.
$conn->on( disconnect => sub { my $disconnectPlayer = $self->{players}->getPlayerWithSocket( +$conn ); $SIG{ALRM} = sub { $self->playerDisconnected() }; $self->{playerDisconnected} = 1; $self->{disconnectedPlayerSocket} = $conn; alarm( 1 ); } );
So the bottom line is, why would the alarm be affected by having an incoming web socket connection from a process on the local host?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Perl alarm not going off when local websocket connection is active
by Fletch (Bishop) on Apr 27, 2025 at 12:31 UTC | |
by cavac (Prior) on May 05, 2025 at 14:45 UTC | |
by jagordon59 (Initiate) on Apr 29, 2025 at 18:50 UTC | |
by Anonymous Monk on Apr 29, 2025 at 21:02 UTC | |
|
Re: Perl alarm not going off when local websocket connection is active
by NERDVANA (Priest) on Apr 30, 2025 at 04:14 UTC | |
by ikegami (Patriarch) on Apr 30, 2025 at 11:06 UTC | |
|
Re: Perl alarm not going off when local websocket connection is active
by NERDVANA (Priest) on Apr 30, 2025 at 20:31 UTC |