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)
----------------------------如何在客戶端斷開連接時刪除訂閱?
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);
}
)
如果聊天給大家,發佈到公共話題
如果聊天指定的客戶端,發佈到privite話題
----------------------------
Q1 : 我需要一個功能,當客戶端斷開服務器時,更新訂戶。 我添加connectionLost方法,但它不起作用。 我打印參數「reason」,我發現這個函數同時刪除public和privite主題。 但我只需要刪除privite主題,我是對不對?但是,如何做到這一點? ----------------------------
問題二:如何廣播一條消息告訴大家,clientX已經離開或clientY有logined? 我不知道如何實現這個功能。