2017-06-07 12 views
-3

我已經做了Java RMI應用程序 - 聊天。Java中的Rmi - 需要修改的聊天應用程序,但是如何?

聊天類別:

public class Chat extends UnicastRemoteObject implements ChatInterface { 

    public String name; 
    public ChatInterface client=null; 

    public Chat(String n) throws RemoteException { 
     this.name=n; 
    } 
    public String getName() throws RemoteException { 
     return this.name; 
    } 

    public void setClient(ChatInterface c){ 
     client=c; 
    } 

    public ChatInterface getClient(){ 
     return client; 
    } 

    public void send(String s) throws RemoteException{ 
     System.out.println(s); 
    } 
} 

ChatClient類:

import java.rmi.*; 
    import java.rmi.server.*; 
    import java.util.*; 

    public class ChatClient { 
     public static void main (String[] argv) { 
      try { 
       System.setProperty("java.rmi.server.hostname","127.0.0.1"); 
       System.setSecurityManager(new RMISecurityManager()); 
       Scanner s=new Scanner(System.in); 
       System.out.println("Enter Your name and press Enter:"); 
       String name=s.nextLine().trim(); 
       ChatInterface client = new Chat(name); 
       ChatInterface server = (ChatInterface)Naming.lookup("rmi://localhost/ABC"); 
       String msg="["+client.getName()+"] got connected"; 
       server.send(msg); 
       System.out.println("[System] Chat Remote Object is ready:"); 
       server.setClient(client); 

       while(true){ 
        msg=s.nextLine().trim(); 
        msg="["+client.getName()+"] "+msg;     
        server.send(msg); 
       } 

      }catch (Exception e) { 
       System.out.println("[System] Server failed: " + e); 
      } 
     } 
    } 

ChatInterface接口:

public interface ChatInterface extends Remote{ 
    public String getName() throws RemoteException; 
    public void send(String msg) throws RemoteException; 
    public void setClient(ChatInterface c)throws RemoteException; 
    public ChatInterface getClient() throws RemoteException; 
} 

的ChatServer類:

public class ChatServer { 
    public static void main (String[] argv) { 
     try { 
      System.setProperty("java.rmi.server.hostname","127.0.0.1"); 
      System.setSecurityManager(new RMISecurityManager()); 
      Scanner s=new Scanner(System.in); 
      System.out.println("Enter Your name and press Enter:"); 
      String name=s.nextLine().trim(); 

      Chat server = new Chat(name); 

      Naming.rebind("rmi://localhost/ABC", server); 

      System.out.println("[System] Chat Remote Object is ready:"); 

      while(true){ 
       String msg=s.nextLine().trim(); 
       if (server.getClient()!=null){ 
        ChatInterface client=server.getClient(); 
        msg="["+server.getName()+"] "+msg; 
        client.send(msg); 
       } 
      } 
     }catch (Exception e) { 
      System.out.println("[System] Server failed: " + e); 
     } 
    } 
} 

聊天 - 相同的代碼在兩個項目

ChatInterface - 相同的代碼在這兩個項目太

ChatClient - 的ChatServer - 他們那些有不同的代碼


它看起來像這樣:How it looks inside Eclipse (hierarchy)

問題是,這個聊天的工作原理是... 我們得到了所有消息(如整個歷史記錄),但用戶發送新消息時。 例如,我們有user1,user2,user3。

  • 用戶1發送消息:'FUN !!!'
  • User2和User3什麼也看不到。
  • 用戶2發送「okkk」
  • 現在user1仍然只能看到「FUN !!!」
  • User2看到「FUN !!!」和「okkk」

而User3什麼都看不到,因爲cuz沒有發送任何消息,所以什麼也沒有。

這很糟糕:/。這就像用戶必須發送一些東西來獲取消息。即使只有「空間」,但還是有些東西。否則他不會收到任何消息。

我需要這樣做,當它由一個用戶發送時,自動發送所有用戶的新消息。像「更新」。「

它需要像

  • 用戶1啥都沒
  • 用戶2啥都沒
  • 用戶3啥都沒
  • Users3發送 'AAAA'
  • 所有用戶現在已經 'AAAA'

回答

0
public interface ChatInterface extends Remote{ 
    public String getName() throws RemoteException; 
    public void send(String msg) throws RemoteException; 
    public void setClient(ChatInterface c)throws RemoteException; 
    public ChatInterface getClient() throws RemoteException; 
} 

問題從這裏開始。 setClient()的性質是服務器一次只知道一個客戶端。當然,服務器應該有一個列表的客戶,方法應該是addClient(ChatInterface c)

作爲遠程方法的getClient()的目的不見了。

+0

我開始明白了。以及如何將此消息發送給所有客戶?所以他們聊天更新? – krystian2160

+0

迭代客戶端列表。 – EJP