[英]Iterator logic issue when creating new RMI chat server room
我从事一个研究项目已有很长时间了,我认为阻碍我前进的一件事是不知道如何正确地为一些旨在创建新聊天室或连接的代码编写循环逻辑用户到一个现有的。
即使房间名称相同,此代码似乎也会在每次有人连接时创建一个新房间。 我很确定这种方法是完全错误的,但是不确定从哪里开始正确使用它。 任何帮助都会很棒。 即使是建议使用其他方法。
public void connect(RMIChatClient theClient, String roomName)
throws RemoteException {
// check if room exists and pass the client to the relevant room
// cycle list of rooms
Iterator<RMIRoomImpl> it = myRoomsList.iterator();
while (it.hasNext()) {
RMIRoomImpl roomTemp = (RMIRoomImpl) it.next();
if (roomTemp.getName() == roomName) {
//if there is a match then just add the client to the room
roomTemp.addClient(theClient);
System.out.println("Bound Client: " + theClient + "in Existing Room:"
+ roomName);
match = true;
return;
}
}
if (match != true) {
// if there is no match then create a new room and pass the first client
RMIRoomImpl newRoom = new RMIRoomImpl(roomName, theClient);
System.out.println("new room created: " + roomName);
myRoomsList.add(newRoom);
System.out.println("Bound Client: " + theClient + "in Room:"
+ roomName);
}
}
你有:
if (roomTemp.getName() == roomName)
你的意思是:
if (roomTemp.getName().equals(roomName))
如果名称不区分大小写,请使用equalsIgnoreCase()
。 如果前导/尾随空格是一个问题,请首先使用trim()
。
顺便说一句,如果RMIRoomImpl
的名称从未更改,则可以找到Map<String,RMIRoomImpl>
更加方便。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.