Skip to content

Commit 7631c33

Browse files
authored
encode player name before sending to game (#138)
1 parent 66b89ae commit 7631c33

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

services/client/src/hooks/useGame.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ const useGame = (gameSocketURL: string, durationInSeconds: number) => {
174174
if (gameState === GameState.NotStarted) {
175175
setPlayerName(playerName);
176176
setGameMode(gameMode);
177-
sendMessage(Event.Start, [playerName, gameMode].join(","));
177+
sendMessage(Event.Start, [encodeURIComponent(playerName), gameMode].join(","));
178178
setGameState(GameState.InGame);
179179
startTimer();
180180
}

services/game/src/main/java/io/openliberty/spacerover/game/websocket/server/GameServer.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2022 IBM Corporation and others.
2+
* Copyright (c) 2022, 2023 IBM Corporation and others.
33
* All rights reserved. This program and the accompanying materials
44
* are made available under the terms of the Eclipse Public License v1.0
55
* which accompanies this distribution, and is available at
@@ -11,6 +11,9 @@
1111
package io.openliberty.spacerover.game.websocket.server;
1212

1313
import java.io.IOException;
14+
import java.io.UnsupportedEncodingException;
15+
import java.net.URLDecoder;
16+
import java.nio.charset.StandardCharsets;
1417
import java.util.Arrays;
1518
import java.util.logging.Level;
1619
import java.util.logging.Logger;
@@ -143,6 +146,12 @@ private String getErrorMessage(final String errorText) {
143146

144147
private void startGame(final String[] properties) {
145148
String playerId = properties[0];
149+
try {
150+
playerId = URLDecoder.decode(playerId, StandardCharsets.UTF_8.name());
151+
} catch (UnsupportedEncodingException e) {
152+
// utf-8 always supported
153+
}
154+
146155
int gameMode = Integer.parseInt(properties[1]);
147156
LOGGER.log(Level.INFO, "Start Game received for player ID: {0}, GameMode: {1}",
148157
new Object[] { playerId, gameMode });

0 commit comments

Comments
 (0)