【发布时间】: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