简体   繁体   中英

keep history of connected clients to a web-service jax-ws

I have a multi player game and I want to keep history of connected players to understand when should game start . I used HttpSession But it can't solve my problem. also I created a static ArrayList of players in webservices but it couldn't solve my poblem . :( Any other idea ?

     @Resource
    private WebServiceContext wsContext;    
   @WebMethod
    public PlayerModel connect(String PlayerName) {
        PlayerModel playerModel = null;
        Players players = new Players() ;
        ArrayList<PlayerModel> playerList = players.getPlayers();

        if (playerList.isEmpty()) {
            System.out.println("from service :No payer yet");
             boardModel = new BoardModel(6, 7);
            session.setAttribute("boardModel", boardModel);



            boardModel.setPlayer1(PlayerName);
            playerModel = new PlayerModel(1, PlayerName, boardModel);
            playerList.add(playerModel) ;
        } else if (playerList.size() == 1) {
            boardModel.setPlayer2(PlayerName);
            playerModel = new PlayerModel(2, PlayerName, boardModel);
          playerList.add(playerModel) ;
        }
        return playerModel;

    }

another approach

 @Resource
 private WebServiceContext wsContext;   
 BoardModel boardModel;
 HttpSession session;

@WebMethod
public PlayerModel connect(String PlayerName) {
    MessageContext mc = wsContext.getMessageContext();
    session = ((HttpServletRequest) mc.get(MessageContext.SERVLET_REQUEST)).getSession();

    if (session == null) {
        System.out.println("from service :No payer yet");
         boardModel = new BoardModel(6, 7);
         session.setAttribute("boardModel", boardModel);
        boardModel.setPlayer1(PlayerName);
        playerModel = new PlayerModel(1, PlayerName, boardModel);
       session.setAttribute("1", playerModel);
    } else if (session.getAttribute("1" != null) {
        boardModel.setPlayer2(PlayerName);
        playerModel = new PlayerModel(2, PlayerName, boardModel);
        session.setAttribute("2", playerModel);
    }
    System.out.println("from service  : player" + PlayerName);
    return playerModel;

}

Your code doesn't seem to add up in multiple places.

  1. Your first code block, apart from missing important bits, couldn't possibly work across multiple requests.

    • playerList looks like it'll almost always be empty
  2. A session-aware JAX-WS service by itself wouldn't help you. The session, by definition is relating to a single user. Without extra legwork on your part, there's no way for one user's webservice session to be shared across multiple users


The one glaring gap in your code is that you're not identifying an instance of the game . When a player requests to connect, they should be requesting to either connect to an existing game or to create one, by passing a gameId . Without a token identifying a specific game, you'll struggle to connect multiple users to the same game. connect should have the (overly simplistic) signature connect(String gameId, String playerName) . A list of gameIds can be managed by a DAO, outside of the context of the webservice endpoint. This way, returning players or new players can make their presence known by presenting a known gameId , and you can keep track of connected users to a specific gameId

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM