Вопрос

I'm making a java swing app using RMI and I would like to know when the client called a method of the server so that other clients get updated. Let me be more specific, i'm making a tic tac toe game in java swing, I have the server and 2 clients, I want that when a client makes a move the other client in its board get the new move. Here's what happens when a client press a button to make a move on one client.

private void btn11ActionPerformed(java.awt.event.ActionEvent evt) {                                      
    try{
        int turno = stub.getTurno();
        int resultado = stub.tirar(1, 1);
        hacerTablero(stub.getPosiciones());
        if (resultado != 3){
            if (turno == 1){
                lblJugador.setText("Turn of player 2");
            }else{
                lblJugador.setText("Turn of player 1");
            }
            if(resultado == 1 || resultado == 2){
                if (resultado == 1){
                    if (turno == 1)
                        JOptionPane.showMessageDialog(this, "Player 1 wins");
                    else
                        JOptionPane.showMessageDialog(this, "Player 2 wins");
                } else
                    JOptionPane.showMessageDialog(this, "It's a tie");
                InterfazGato nuevo=new InterfazGato();
                nuevo.setVisible(true);
                this.dispose();
            }
        } else {
            JOptionPane.showMessageDialog(this, "This place is taken");
        }
    }catch(RemoteException | HeadlessException e){
        System.out.println(Exception from the remote method: " + e.getMessage());
    }
}

In the line

int resultado = stub.tirar(1, 1);

is where I call the method on the server side on the remote object, and here is this method

public int tirar(int x, int y) throws RemoteException{
    if (gato[x-1][y-1] == 0){
        if (turno == 1){
            gato[x-1][y-1] = 1;
            if (ganar()== 1){
                reiniciar();
                return 1;
            } else if (ganar() == 2){
                reiniciar();
                return 2;
            }
            turno = 2;
        }
        else{
            gato[x-1][y-1] = 2;
            if (ganar()== 1){
                reiniciar();
                return 1;
            } else if (ganar() == 2){
                reiniciar();
                return 2;
            }
            turno = 1;
        }
    } else
        return 3;
    return 0;
}

Everytime the method tirar(int, int) is called, it checks the matrix on the remote object with the moves and positions the players have made. The method ganar() is to know when the game is finished, it returns 0 if it hasnt fininshed, 1 if somebody won, 2 if its a tie.

On the other hand I still don't know how many clients are connected and I would like to be 2, if theres one that the app wait until theres 2 and if a third cient tries to make a connection refuse it.

Any help would be greately appreciated, I've been looking for an answer for some time now and I haven't found anything.. This is my last option..

P.S. I hope I make myself clear, english is not my first language

Это было полезно?

Решение

I don't know if it was the right approach but to solve my problem I use a timer on both clients, so every certain time the clients checked for updates on the server, so the clients get updated if there was any and gave the appearence they knew when the other client had made a move. That made the trick for me.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top