class MyServerProtocol(WampServerProtocol):
@exportRpc
def getSubscribers(self):
print "call getSubscribers"
subscriptions = []
for key, value in enumerate(self.factory.subscriptions):
subscriptions.append(value)
return json.dumps(subscriptions)
def onSessionOpen(self):
print "someone has logined"
self.registerForPubSub("http://example.com/event/", True)
self.registerMethodForRpc("http://example.com/event/getSubscribers",
self,
MyServerProtocol.getSubscribers)
def connectionLost(self, reason):
print "someone disconnect", reason
self.factory._unsubscribeClient(self)
self.factory._removeSession(self)
WampProtocol.connectionLost(self, reason)
WebSocketServerProtocol.connectionLost(self, reason)
——————————————————————————————</p>
ab.connect(chat.wsuri,
function (session) {
sess = session;
sess.prefix("event", chat.prefix);
sess.subscribe("event:" + chat.channel, chat.subscribeSuccess); //public topic, subscribe a common topic
sess.subscribe("event:" + chat.username, chat.subscribeSuccess);//privite topic, subscribe myself topic
chat.stateUpdate(true);
},
function (code, reason) {
sess = null;
alert(reason);
chat.stateUpdate(false);
}
)
如果与所有人聊天,则发布到公共主题
如果与指定的客户聊天,则发布到私人主题
——————————————————————————————</p>
Q1:我需要一个功能,当客户端断开服务器时,更新订阅者。我添加了 connectionLost 方法,但它不起作用。我打印参数“原因”,我发现这个功能同时删除了公共和私人主题。但我只需要删除私人主题,对吗?但是,如何做到这一点?——————————————————————————————</p>
Q2:如何广播消息告诉大家,clientX已经离开或者clientY已经登录?我不知道如何实现这个功能。