Skip to content

Commit

Permalink
Implement PING/PONG for WebSockets to keep Heroku alive (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
neelsomani authored Apr 28, 2020
1 parent 563671f commit a48c4ef
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 5 deletions.
17 changes: 14 additions & 3 deletions backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ def register_with_uuid(self, player_uuid):
"""
Send the client the info for the specified `player_uuid`.
The `player_uuid` must already be present in `self.players`.
Send the player names if they have updated.
"""
player_n = self.users[player_uuid].player_n
payload = {
Expand All @@ -191,7 +193,8 @@ def register_with_uuid(self, player_uuid):
self.logger.info('Sent registration to user {}'.format(player_uuid))
username = self.users[player_uuid].username
if self.player_names.get(player_n) != username:
self.player_names[player_n] = username
if player_n != VISITOR_PLAYER_ID:
self.player_names[player_n] = username
self._send_player_names()

if self.current_players == self.n_players:
Expand Down Expand Up @@ -224,7 +227,8 @@ def _send(self, client, data):
self.logger.info(
'Player {} is disconnected from game {}'
.format(player_uuid, self.uuid))
self.player_names[u.player_n] = u.username
if u.player_n != VISITOR_PLAYER_ID:
self.player_names[u.player_n] = u.username
self._send_player_names()

def _send_all(self, message):
Expand All @@ -250,12 +254,19 @@ def _fill_bots(self, message):
util.BotClient(),
'Bot {}'.format(self.current_players + i))

def _ping_pong(self, payload):
user = self.users[payload['key']]
gevent.spawn(self._send, user.socket, {
'action': PING_PONG
})

def handle_message(self, message):
action_map = {
CLAIM: self._claim,
MOVE: self._move,
SWITCH_TEAM: self._switch_team,
START_GAME: self._fill_bots
START_GAME: self._fill_bots,
PING_PONG: self._ping_pong
}
fn = action_map.get(message['action'])
if fn is None:
Expand Down
1 change: 1 addition & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@
SWITCH_TEAM = 'switch_team'
START_GAME = 'start_game'
PLAYER_NAMES = 'player_names'
PING_PONG = 'ping_pong'
2 changes: 2 additions & 0 deletions src/components/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,5 @@ export const UNCLAIMED = 'unclaimed';

export const PLAYER_UUID = 'player_uuid';
export const PLAYER_NAME = 'player_name';

export const PING_PONG_INTERVAL_MS = 5000
16 changes: 15 additions & 1 deletion src/views/Game.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
UNCLAIMED,
SET_NAME_MAP,
PLAYER_UUID,
PLAYER_NAME
PLAYER_NAME,
PING_PONG_INTERVAL_MS
} from '../components/Constants';
import './Game.css';

Expand All @@ -40,6 +41,17 @@ class Game extends Component {
};
const audioUrl = process.env.PUBLIC_URL + '/bell.mp3';
this.bell = new Audio(audioUrl);
setInterval(this.pingPong.bind(this), PING_PONG_INTERVAL_MS)
}

pingPong() {
this.sendMessage({
'action': 'ping_pong',
'game_uuid': this.state.gameUuid,
'payload': {
'key': this.state.uuid
}
})
}

defaultNames() {
Expand Down Expand Up @@ -246,6 +258,8 @@ class Game extends Component {
case 'player_names':
this.playerNames(data.payload)
break;
case 'ping_pong':
break;
default:
throw new Error('Unhandled action: ' + data.action);
}
Expand Down
11 changes: 10 additions & 1 deletion test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,20 @@ def test_registration(api):
assert len(c.messages) == 2
msg = _action_from_messages(c.messages, REGISTER)
assert msg['player_n'] != VISITOR_PLAYER_ID
assert 'player_uuid' in msg
player_uuid = msg['player_uuid']
game_uuid = msg['game_uuid']
assert msg['time_limit'] == TIME_LIMIT
assert msg['n_players'] == N_PLAYERS
msg = _action_from_messages(c.messages, PLAYER_NAMES)
assert msg['names']['0'] == MOCK_NAME
api.handle_message({
'action': PING_PONG,
'game_uuid': game_uuid,
'payload': {
'key': player_uuid
}
})
assert c.messages[-1]['action'] == PING_PONG


def _action_from_messages(messages, action):
Expand Down

0 comments on commit a48c4ef

Please sign in to comment.