简体   繁体   English

知道客户端何时调用远程对象RMI

[英]Know when a client called a remote object RMI

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. 我正在使用RMI制作一个Java swing应用程序,我想知道客户端何时调用服务器的方法,以便其他客户端得到更新。 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. 让我更具体一点,我正在用Java swing做一个井字游戏,我有服务器和2个客户端,我希望当一个客户端进行移动时,董事会中的另一个客户端获得新的移动。 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. 每次调用tirar(int,int)方法时,它都会检查玩家做出的动作和位置,从而检查远程对象上的矩阵。 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. ganar()方法要知道游戏何时结束,如果尚未完成,则返回0,如果有人赢得,则返回1,如果并列,则返回2。

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. 另一方面,我仍然不知道连接了多少个客户端,而我想成为2个,如果有一个客户端等待应用程序等待到2个客户端,并且有三分之一的人试图建立连接拒绝它。

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.. 任何帮助将不胜感激,我一直在寻找答案已有一段时间,但我没有发现任何东西。.这是我的最后选择。

PS I hope I make myself clear, english is not my first language PS我希望我能说清楚,英语不是我的母语

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 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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