Java 中的 Rmi - 需要修改的聊天应用程序,但是如何修改?
Rmi in Java - Chat Application with need of modification, but How?
我已经制作了 java RMI 应用程序 - 聊天。
聊天Class:
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);
}
}
聊天客户端class:
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;
}
聊天服务器class:
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);
}
}
}
Chat - 两个项目中的相同代码
ChatInterface - 两个项目中的代码也相同
ChatClient - ChatServer - 它们具有不同的代码
看起来像这样:How it looks inside Eclipse (hierarchy)
问题是,这个聊天功能类似于...
我们收到了所有消息(比如整个历史记录)但是当用户发送新消息时。
比如我们有user1,user2,user3.
- User1 发送消息:'FUN!!!'
- User2 和 User3 什么也看不到。
- 用户2发送"okkk"
- 现在 user1 仍然只能看到 "FUN!!!"
- User2 参见 "FUN!!!" 和 "okkk"
而 User3 什么也看不到,因为没有发送任何消息,所以什么也没有。
这很糟糕:/。这就像用户必须发送一些东西才能收到消息。即使只有"space",但还是有的。否则他不会收到任何消息。
我需要让它在一个用户发送新消息时自动向所有用户发送新消息。喜欢"update".'
需要像
- User1 什么都没得到
- User2 什么都没得到
- User3 什么都没得到
- Users3 发送 'AAAA'
- 所有用户现在 'AAAA'
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()
的本质是服务器一次只知道一个客户端。服务器当然应该有一个 list 客户端,方法应该是 addClient(ChatInterface c)
.
getClient()
作为远程方法的目的让我难以理解。
我已经制作了 java RMI 应用程序 - 聊天。
聊天Class:
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);
}
}
聊天客户端class:
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;
}
聊天服务器class:
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);
}
}
}
Chat - 两个项目中的相同代码
ChatInterface - 两个项目中的代码也相同
ChatClient - ChatServer - 它们具有不同的代码
看起来像这样:How it looks inside Eclipse (hierarchy)
问题是,这个聊天功能类似于... 我们收到了所有消息(比如整个历史记录)但是当用户发送新消息时。 比如我们有user1,user2,user3.
- User1 发送消息:'FUN!!!'
- User2 和 User3 什么也看不到。
- 用户2发送"okkk"
- 现在 user1 仍然只能看到 "FUN!!!"
- User2 参见 "FUN!!!" 和 "okkk"
而 User3 什么也看不到,因为没有发送任何消息,所以什么也没有。
这很糟糕:/。这就像用户必须发送一些东西才能收到消息。即使只有"space",但还是有的。否则他不会收到任何消息。
我需要让它在一个用户发送新消息时自动向所有用户发送新消息。喜欢"update".'
需要像
- User1 什么都没得到
- User2 什么都没得到
- User3 什么都没得到
- Users3 发送 'AAAA'
- 所有用户现在 'AAAA'
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()
的本质是服务器一次只知道一个客户端。服务器当然应该有一个 list 客户端,方法应该是 addClient(ChatInterface c)
.
getClient()
作为远程方法的目的让我难以理解。