【问题标题】:Know when a client called a remote object RMI知道客户端何时调用远程对象 RMI
【发布时间】:2013-11-12 17:52:24
【问题描述】:

我正在使用 RMI 制作一个 java swing 应用程序,我想知道客户端何时调用服务器的方法以便其他客户端得到更新。 让我更具体地说,我正在用 java swing 做一个井字游戏,我有服务器和 2 个客户端,我希望当一个客户端移动时,它的板上的另一个客户端得到新的移动。以下是当客户按下按钮以在一个客户上移动时发生的情况。

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());
    }
}

排队

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

是我在远程对象上调用服务器端方法的地方,这里是这个方法

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;
}

每次调用 tirar(int, int) 方法时,它都会检查远程对象上的矩阵以及玩家所做的移动和位置。 方法 ganar() 用于知道游戏何时结束,如果还没有结束,则返回 0,如果有人赢了,则返回 1,如果是平局,则返回 2。

另一方面,我仍然不知道有多少客户端已连接,我想成为 2,如果有一个应用程序等到 2 并且如果第三个 cient 尝试建立连接拒绝它。

任何帮助将不胜感激,我一直在寻找答案已经有一段时间了,但我没有找到任何东西..这是我最后的选择..

附:我希望我说清楚,英语不是我的第一语言

【问题讨论】:

    标签: java swing rmi remoteobject


    【解决方案1】:

    我不知道这是否是正确的方法,但为了解决我的问题,我在两个客户端上都使用了一个计时器,因此客户端每次都会检查服务器上的更新,因此客户端会在有更新时得到更新,并且当另一个客户采取行动时,他们会做出他们知道的样子。这对我来说是个窍门。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-25
      • 2010-10-10
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 2015-10-12
      • 2014-08-19
      • 1970-01-01
      相关资源
      最近更新 更多