2016-10-20 29 views
0

我正在建立一個Websocket服務器,它登錄到另一個服務器並通過套接字將數據推送到網頁(通過訂閱功能)。只要我一直從websocket運行的文件調用廣播函數,一切都很好。但是從另一個python文件調用廣播方法,其中我的推送功能正在打印到命令行,沒有客戶端正在收到消息。扭曲的WebSocket - 從另一個文件調用時廣播不起作用

我認爲,從另一個文件調用廣播會創建另一個實例,並且self.clients爲空。 所以總結一下,連接的客戶端從loginGESI()獲得廣播,但不在scrptCallbackHandlerExample(subType)的第二個文件中。

對任何幫助感到高興!

這裏是我的WebSocket文件:

class BroadcastServerProtocol(WebSocketServerProtocol): 

    def onOpen(self): 
     self.factory.register(self) 

    def connectionLost(self, reason): 
     WebSocketServerProtocol.connectionLost(self, reason) 
     self.factory.unregister(self) 

class BroadcastServerFactory(WebSocketServerFactory): 
    clients = [] 

    def __init__(self, url): 
     WebSocketServerFactory.__init__(self, url) 

    def register(self, client): 
     if client not in self.clients: 
      print("registered client {}".format(client.peer)) 
      self.clients.append(client) 

    def unregister(self, client): 
     if client in self.clients: 
      print("unregistered client {}".format(client.peer)) 
      self.clients.remove(client) 

    @classmethod 
    def broadcast(self, msg): 
     print("broadcasting message '{}' ..".format(msg)) 
     print(self.clients) 
     for c in self.clients: 
      c.sendMessage(msg.encode('utf8')) 
      print("message sent to {}".format(c.peer)) 

def login(): 
    codesys = Test_Client("FTS_test") 
    result = codesys.login() 
    # FTS = codesys.searchForPackage("F000012") 
    FTS = ["15900"]; 
    scrptContextId = [None] * len(FTS) 
    itemContextIds_array = [None] * len(FTS) 
    for i in range(0,len(FTS)): 
     result, scrptContextId[i] = codesys.createSubscription(c_ScrptCallbackHandlerExample, 100, int(FTS[i])) 
     print("SubscriptionRoomId: "+str(scrptContextId[i])) 
     result, itemContextIds_array[i], diagInfo = codesys.attachToSubscription(1, [FTS[i]+'.speed'], [100]) 
     print("Subscription done for: "+str(itemContextIds_array[i])) 
     print("Subscription for: Speed") 

    BroadcastServerFactory.broadcast(str(FTS[0])) 


if __name__ == '__main__': 

    # Logger Websocket 
    log.startLogging(sys.stdout) 
    # factory initialisieren 
    ServerFactory = BroadcastServerFactory 
    factory = ServerFactory("ws://127.0.0.1:9000") 
    factory.protocol = BroadcastServerProtocol 
    listenWS(factory) 
    # reactor initialisieren 
    webdir = File(".") 
    web = Site(webdir) 
    reactor.listenTCP(8080, web) 

    reactor.callLater(5, login) 

    reactor.run() 

,在這裏我的簽約檔案:

# Launch of the CallbackHandler named in the createSubscription function 
# CallbackHandler describes what happens to a variable which changes its value 
def scrptCallbackHandlerExample(subType): 
    BroadcastServerFactory.broadcast('test') 

    # Saves the value of the variables(s) in an array 
    dataValue = [] 
    for i in range(0,subType.size): 
     dataValue.append(subType.dataItems[i].node.dataValue) 

    # Print variabel informations on the screen 
    print "*****Callback - Data Change in a Variable*****" 
    print('Subscription ID: %d' % subType.subscrId) 
    for idx in range(0,subType.size): 
     print('** Item %d **' % idx) 
     print('Item Id: %d' % subType.dataItems[idx].dataItemId) 
     print('Item Node ID: %s' % subType.dataItems[idx].node.nodeId) 
     print('Item data value: %s' % subType.dataItems[idx].node.dataValue) 
     print('Item data type: %s' % subType.dataItems[idx].node.dataType) 
     print('******************************') 
# Define the type of the function as an eSubscriptionType 
CB_FUNC_TYPE = CFUNCTYPE(None, eSubscriptionType) 
c_ScrptCallbackHandlerExample = CB_FUNC_TYPE(scrptCallbackHandlerExample) 

問候

+0

它不這樣工作。其他腳本將作爲其他進程運行,並且無法訪問其他進程。你需要一種IPC來做到這一點。 https://docs.python.org/2/library/ipc.html –

+0

我也在閱讀。我不能通過全局變量進行通信嗎?我嘗試過但仍然無法工作....另一個想法是將其他python文件包含到我的套接字文件中,但後來出現EOFError 10054:現有連接被遠程主機強行關閉。畢竟,我不得不承認,我對編程和python完全不熟悉,現在IPC對我來說可能太多了,是不是有一個更簡單的解決方案? – Caligula

+0

只是爲了更清楚地說明:共享代碼的兩個獨立流程就像是兩輛車型相同的前照燈。如果你打開一個燈,在其他車上絕對沒有任何事情發生。 –

回答

0

我發現,在我oppinion,一個漂亮整潔的解決方法。

每當我的訂閱函數被調用時,我會連接一個本地客戶端到我的websocket服務器,並向他發送一個帶有新值的消息,然後websocket服務器將這個消息推送給其他客戶端。由於我仍在處理它,所以我不能發佈任何代碼,但是已經給出了功能。所以如果有人對解決方案感興趣,讓我知道,我可以發佈我的。

相關問題