gpt4 book ai didi

java RMI概念

转载 作者:行者123 更新时间:2023-12-01 15:52:42 24 4
gpt4 key购买 nike

我正在尝试实现一个简单的聊天应用程序,该应用程序通过中央服务器将客户端与其他客户端连接起来,以便它们可以交换消息和文件。我还需要实现一个通知框架。例如,如果用户成功登录,或者他的好友登录,他会收到通知。现在在 RMI 世界中这是如何实现的?我正在考虑有一个远程对象“连接类”,客户端可以从中调用方法,例如“登录”,“断开连接”等......至于通知框架类,它们也必须是远程的吗?或者它们可以存在于服务器中吗? 谢谢

最佳答案

远程系统之间的事件消息传递有点棘手。以下是必须发生的事情:

  • 客户端必须对服务器端触发的事件感兴趣。要注册,客户端必须可远程访问事件源对象。

  • 为了能够注册,客户端必须首先找到服务器,因此服务器对象必须可供客户端远程使用。

恶心,对吧?这就是简单的pattern用于实现远程事件处理。几周前,我开始了一个沿着这条路走下去的教程——它就在这里,我希望在本周末之前添加一些东西。唉,租金的需要已经受到干扰,我无法尽快添加它。但是,如果您等不及,那就是关键:双方都必须远程可用,消息系统才能正常工作。

服务器和客户端必须是远程对象。

让所有客户端实现远程接口(interface)。

RemoteClientIfc extends Remote {
void inform();
}

//have a remote method register() on the *Server* object which accepts RemoteClientIfc.
//c'd be something like this...
register(RemoteClientIfc client){
arrayListofClients.add(client);
}

//So client will do a look up on the remote server object and register itself.
remoteObj.register(clientInstance);

//In the remote server you
//can probably have another method to send notifications to the client.
//Run through the ArrayList and call
//inform() on each of them.
//Thus the client will receive notification.
tellClients(){
Iterator i = ....
while (i.hasNext()){
((RemoteClientIfc).i.next()).inform();
}
}

关于java RMI概念,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5692457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com