如何从不同的类推送消息? (EE7 Websocket)

时间:2017-12-07 08:11:36

标签: java websocket java-websocket

我已经使用Java EE 7提供的API为使用HTML5 websockets的游戏实现了websocket端点。我可以使用计时器任务成功发送消息,以更新球位置,砖块等,但我希望能够将我的Game类中的消息发送到正确的播放器,并在游戏丢失时发送消息,当分数更新时,......

现在唯一可行的方法是在每次更新时发送得分,amountOfLives,......但我觉得这不是可行的方法。是否可以从不同的类发送消息到客户端,在本例中是我的Game类?

游戏课(请参阅评论我想要达到的目标)

public class Game implements Runnable {

    private void loseLife() {
        if (lives != 0) {
            lives -= 1;
            ball.reset(paddle);
        } else {
            lose();
        }
    }

    private void lose() {
        gameRunning = false;

        // What I would like to achieve
        sendMessage("Game is lost");
    }
}

套接字端点:

@ServerEndpoint("/singleplayer")
public class SingleplayerSocket {

    private static final Set<Session> PLAYERS = Collections.synchronizedSet(new HashSet<Session>());

    private Session session;
    private Gson gson;
    private Game game;

    private void sendMessage(String message) {
        try {
            for (Session player : PLAYERS) {
                if (player == session) {
                    player.getBasicRemote().sendText(message);
                }
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    @OnOpen
    public void onOpen(Session session) {
        this.session = session;

        PLAYERS.add(session);
        sendMessage("Connection established");
    }

    @OnMessage
    public void onMessage(String message) {
        if (session != null && session.isOpen()) {
            gson = new Gson();

            String messageType = gson.fromJson(message, MessageType.class).getMessage();

            switch (messageType) {

                case "gameSetup":
                    String jsonData = getJsonData(message);
                    GameSetupRequest request = gson.fromJson(jsonData, GameSetupRequest.class);

                    int canvasWidth = request.getCanvasWidth();
                    int canvasHeight = request.getCanvasHeight();
                    String difficultyName = request.getDifficulty();

                    sendMessage(gameSetup(canvasWidth, canvasHeight, difficultyName));
                    break;

                case "gameStart":
                    Thread gameLoop = new Thread(game);
                    gameLoop.start();

                    Timer timer = new Timer();
                    timer.schedule(new UpdateTask(), 0, 1000 / 60);

                    sendMessage("Game started");
                    break;
        }
    }

    @OnClose
    public void onClose(Session session) {
        PLAYERS.remove(session);
        this.session = null;
    }

    class UpdateTask extends TimerTask {

        @Override
        public void run() {
            if (game.isGameRunning()) {
                sendMessage(gameUpdate());
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

通过将会话添加到我的构造函数

来解决它
private Session session;

public Game(Session session, int width, int height) {
    this.session = session;
    this.WIDTH = width;
    this.HEIGHT = height;
}


private void sendMessage(String text) {
    try {
        session.getBasicRemote().sendText(text);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

然后生成这样的新游戏:

game = new Game(session, setupRequest.getCanvasWidth(), setupRequest.getCanvasHeight());