0 java spring hashmap websocket
A Map.put() overwrites existing values in itself, despite putting in a unique value.
I am initializing a singleton of an object "Game" in which I have a map of Rooms (and Players).
When I input new key/value pair into the roomsList like this: roomsList.put(uniqueKey, new Room(uniqueKey, name)), the new key/value pair is added into the Map, but the rest of the pairs (each with a unique identifier) have their values overwritten as well.
I have tried putting in a new room already in my Spring Controller with Game.getRoomsList().put() creating a separate object for client Messages so that instead of a Player message, - so the Controller's parameter is of object type NewRoomMessage And even putting the key/value pairs directly in the private Game constructor...
The weird thing is, that with my Map playersList, everything works fine..
lobby.js
This script sends name and generated roomId to the server endpoint /game/sessionId, where sessionId is the websocket identifier of a player, roomId is a newly generated ID and name is the user's input for new room name
function createRoom() {
name = $('#inputRoomName').val();
roomId = '_' + Math.random().toString(36).substr(2, 9);
console.log(roomId);
stompClient.send('/game/'+sessionId, {}, JSON.stringify({'message': name, 'roomId': roomId}));
}
Run Code Online (Sandbox Code Playgroud)
This is my Spring controller which handles messages from Player for and is made for creating Rooms in game RoomsController
@Controller
public class RoomsController {
@MessageMapping("/game/{sessionId}")
@SendTo("/topic/game/{sessionId}")
public RoomMessage createRoom(@DestinationVariable String sessionId, Player player) throws Exception {
Game game = Game.getInstance();
game.addRoom(player);
return new RoomMessage("Room with the ID " + player.getRoomId() + " created");
}
}
Run Code Online (Sandbox Code Playgroud)
This is the Game Object which is a Singleton and with which my Spring server communicates
Game.java
package cz.vse.pavm07.bp.objects;
import java.util.HashMap; import java.util.Map;
import org.springframework.stereotype.Controller;
@Controller
public class Game{
private static Game game;
/*
private static List<Player> players = new ArrayList<Player>();
private static List<Player> playersToRemove = new ArrayList<Player>();
*/
private static Map<String, Player> playersList = new HashMap<String, Player>();
private static Map<String, Player> playersToRemove = new HashMap<String, Player>();
private static Map<String, Room> roomsList = new HashMap<String, Room>();
private static Map<String, Room> roomsToRemove = new HashMap<String, Room>();
// LAZY SINGLETON GAME
private Game() {}
public static Game getInstance() throws Exception{
if(game == null) {
game = new Game();
}
return game;
}
/* This is the method I have issues with, it somehow overwrites already existing values in my Map of Rooms */
/* ROOMS LIST */
public static boolean addRoom(Player player) {
if (!roomsList.containsKey(player.getRoomId())) {
roomsList.put(player.getRoomId(), new Room(player.getRoomId(), player.getMessage()));
return true;
}
return false;
}
/* PLAYERS LISTS */
/* Method adds new player to the playersList and it works right */
public static boolean addPlayer(String sessionId, String name) {
if (playersList.containsKey(sessionId)) {
return false;
} else {
playersList.put(sessionId, new Player(sessionId, name));
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
This is my Room constructor Room.java
public Room (String ID, String name) {
this.ID = ID;
this.name = name;
}
Run Code Online (Sandbox Code Playgroud)
Example output:
NEW ROOM IS ADDED
I am printing out the keyset of my roomsList, and then for each room its key, its ID and its Name
[_y46r22hdu]
The Key of the Room is_y46r22hdu, The ID is _y46r22hdu and the Name of the room isRoom1
NEW ROOM IS ADDED
I am printing out the keyset of my roomsList, and then for each room its key, its ID and its Name
[_jxltglk5z, _y46r22hdu]
The Key of the Room is_jxltglk5z, The ID is _jxltglk5z and the Name of the room isRoom2
The Key of the Room is_y46r22hdu, The ID is _jxltglk5z and the Name of the room isRoom2
Run Code Online (Sandbox Code Playgroud)
I expect the Map to add a new Object without overwriting the already existing key/value pairs...
The link to this project's repository is here: https://github.com/MartinPavelka/chaser-server
Jon*_*eet 14
错误在您的Room课堂上。这两个name和ID是静态字段,而你希望每个实例有一个单独的对场。
所以这:
private static String name;
private static String ID;
Run Code Online (Sandbox Code Playgroud)
应该
private String name;
private String id; // Name changed to follow conventions
Run Code Online (Sandbox Code Playgroud)
我也将亲自制作它们final,以明确它们在对象的整个生命周期中不会发生变化:
private final String name;
private final String id;
Run Code Online (Sandbox Code Playgroud)
您还需要将某些静态方法更改为实例方法...,然后查看其余字段。基本上,您需要注意状态的哪些方面旨在实现类型范围(静态),以及哪些方面针对每个实例实现。(对于Game,您也应该查看相同的问题。从根本上讲,静态字段通常在应用程序中很少见。当最终几乎所有字段都是静态的时(根据当前项目的情况),这表明您需要查看再次设计。)
| 归档时间: |
|
| 查看次数: |
219 次 |
| 最近记录: |