我一直在构建一个聊天应用程序,其中连接到服务器的客户端可以使用 Pyro4 进行私人聊天。客户端注册到服务器以向所有人表明他们可以进行私人聊天。客户端现在可以邀请其他客户端使用 RMI 直接交换消息进行私人聊天会话。下面是我的课程代码。
@Pyro4.expose
@Pyro4.behavior(instance_mode="single")
class Messaging(object):
def __init__(self):
self._connection = ''
self._messages_to_server = []
self._messages_to_client = []
def read_messages_to_server(self):
try:
return self._messages_to_server.pop(0)
except:
return FALSE
def read_messages_to_client(self):
try:
return self._messages_to_client.pop(0)
except:
return FALSE
def send_to_server(self, message):
self._messages_to_server.append(message)
def send_to_client(self, message):
self._messages_to_client.append(message)
def set_connection(self, allow):
self._connection = allow
def get_connection(self):
return self._connection
每当一位客户邀请一位客户进行私人聊天时,我的代码都会在下面重新运行以创建一个 uri 。
daemon = Pyro4.Daemon()
uri_str = daemon.register(Messaging())
daemon.requestLoop()
它运行良好,第一次运行。但是对于第二次私聊请求,却报错:
Pyro4.errors.DaemonError: object or class already has a Pyro id
我该如何解决?我想为建立私有连接的不同客户端创建不同的 URI?