2010-02-02 47 views
4

例如,我有一個連接到服務器具有以下客戶:如何在從pb.Root繼承的服務器中檢測丟失的客戶端連接?

class MyClientFactory(pb.PBClientFactory, ReconnectingClientFactory): 
    def __init__(self): 
     pb.PBClientFactory.__init__(self) 
     self.ipaddress = None 

    def clientConnectionMade(self, broker): 
     log.msg('Started to connect.') 
     pb.PBClientFactory.clientConnectionMade(self, broker) 

    def buildProtocol(self, addr): 
     log.msg('Connected to %s' % addr) 
     return pb.PBClientFactory.buildProtocol(self, addr) 

    def clientConnectionLost(self, connector, reason): 
     log.msg('Lost connection. Reason:', reason) 
     ReconnectingClientFactory.clientConnectionLost(self, connector, reason) 

    def clientConnectionFailed(self, connector, reason): 
     log.msg('Connection failed. Reason:', reason) 
     ReconnectingClientFactory.clientConnectionLost(self, connector, reason) 

所以當連接丟失,客戶端可以自動檢測。

我如何從服務器獲取相同的行爲應該在客戶端下去,例如崩潰?

我現在趕DeadReferenceError,(通過連接的潛在客戶名單迭代),但是這是一個非事件驅動的方式 - 坦率地說爲時已晚。

任何想法表示歡迎。

在此先感謝。

回答

3

你可以註冊一個回調,當連接丟失時被調用。有兩個API,一個是Broker.notifyOnDisconnect,另一個是RemoteReference.notifyOnDisconnect。他們做同樣的事情,但根據應用程序的細節,其中一個或另一個可能更方便訪問。

我不知道,如果你可以用它來保證,你永遠不會得到DeadReferenceError(例如,我不知道如果remote_foo方法,如果你被調用時,你返回遞延會發生什麼,連接丟失,然後延期觸發),但是至少可以大幅度降低常見情況下的可能性(即,如果在notifyOnDisconnect回調函數後從未使用callRemote,則應該始終避免從callRemote獲得DeadReferenceError )。

相關問題