2015-03-01 7 views
5

में चल रहे ऑटोबहन में बाहर से संदेश विधि MyServerProtocol कक्षा से बाहर की ओर से कॉल करना चाहते हैं और कनेक्ट किए गए ग्राहकों को एक संदेश भेजना चाहते हैं। ऐसा करने के लिए मैं threading का उपयोग करता हूं।भेजें अलग-अलग धागे

जब मैं इस कोड का उपयोग:

from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory 
from twisted.internet import reactor 
import threading 

class MyServerProtocol(WebSocketServerProtocol): 
    def onConnect(self, request): 
     print("Client connecting: {0}".format(request.peer)) 

    def onOpen(self): 
     print("WebSocket connection open.") 

    def onMessage(self, payload, isBinary): 
     if isBinary: 
      print("Binary message received: {0} bytes".format(len(payload))) 
     else: 
      print("Text message received: {0}".format(payload.decode('utf8'))) 

     self.sendMessage(payload, isBinary) 

    def onClose(self, wasClean, code, reason): 
     print("WebSocket connection closed: {0}".format(reason)) 


class Connection(threading.Thread): 
    def __init__(self): 
     super(Connection, self).__init__() 

    def run(self): 
     self.factory = WebSocketServerFactory("ws://localhost:9000", debug=False) 
     self.factory.protocol = MyServerProtocol 
     reactor.listenTCP(9000, self.factory) 
     reactor.run(installSignalHandlers=0) 

    def send(self, data): 
     reactor.callFromThread(self.factory.protocol.sendMessage, self.factory.protocol, data) 

connection = Connection() 
connection.daemon = True 
connection.start() 
connection.send('test') 

इस त्रुटि होता है:

connection.send('test') 
reactor.callFromThread(self.factory.protocol.sendMessage, self.factory.protocol, data) 
AttributeError: 'Connection' object has no attribute 'factory' 

अगर मैं लाइन connection.send('test') बाहर टिप्पणी करने के लिए प्रयास करते हैं, इस त्रुटि होता है:

TypeError: 'NoneType' object is not iterable 

क्या मेरे कोड के साथ समस्या है?

क्या मैं इसे सही तरीके से कर रहा हूं? या प्रोटोकॉल कक्षा के बाहर से ग्राहकों को संदेश भेजने का कोई और तरीका है?

धन्यवाद। अपने "init (स्वयं):" करने के लिए

+0

क्या आप कॉल करते समय self.factory मौजूद है? प्रारंभ() के बीच नींद डालने का प्रयास करें और भेजें() और जांचें। इसके अलावा, एक डीबगर का उपयोग करें। – Raito

+0

क्या आपने कभी यह पता लगाया कि यह कैसे करें? मुझे भी यही समस्या हो रही है। – someuser

उत्तर

0

ऐड self.factory नीचे देखें:

from autobahn.twisted.websocket import WebSocketServerProtocol, WebSocketServerFactory 
    from twisted.internet import reactor 
    import threading 

    class MyServerProtocol(WebSocketServerProtocol): 
     def onConnect(self, request): 
      print("Client connecting: {0}".format(request.peer)) 

     def onOpen(self): 
      print("WebSocket connection open.") 

     def onMessage(self, payload, isBinary): 
      if isBinary: 
       print("Binary message received: {0} bytes".format(len(payload))) 
      else: 
       print("Text message received: {0}".format(payload.decode('utf8'))) 

      self.sendMessage(payload, isBinary) 

     def onClose(self, wasClean, code, reason): 
      print("WebSocket connection closed: {0}".format(reason)) 


    class Connection(threading.Thread): 
     def __init__(self,factory): 
      super(Connection, self).__init__() 
      self.factory=WebSocketServerFactory("ws://localhost:9000", debug=False) 
     def run(self): 
      self.factory.protocol = MyServerProtocol() 
      reactor.listenTCP(9000, self.factory) 
      reactor.run(installSignalHandlers=0) 

     def send(self, data): 
      reactor.callFromThread(self.factory.protocol.sendMessage, self.factory.protocol, data) 

    connection = Connection() 
    connection.daemon = True 
    connection.start() 
    connection.send('test') 
+0

क्या यह थ्रेडिंग सहकर्मियों के कई कनेक्शन भी संभालती है? – hietpasd

3

[वहाँ] एक और तरीका सर्वर वर्ग के बाहर से ग्राहकों को संदेश भेजने के लिए है?

मैं संदेश भेजने के लिए ऐसा कुछ करता हूं। मैं अपना वेब ऐप चलाने के लिए twisted का उपयोग करता हूं।

import json 
from autobahn.twisted.websocket import WebSocketServerProtocol 
from twisted.internet import reactor 

class MyProtocol(WebSocketServerProtocol): 
    connections = list() 

    def onConnect(self, request): 
     self.connections.append(self) 

    def onClose(self, wasClean, code, reason): 
     self.connections.remove(self) 

    @classmethod 
    def broadcast_message(cls, data): 
     payload = json.dumps(data, ensure_ascii = False).encode('utf8') 
     for c in set(cls.connections): 
      reactor.callFromThread(cls.sendMessage, c, payload) 


# Somewhere else 
MyProtocol.broadcast_message({'greeting': 'Hello world'}) 

अगर यह है सही रास्ता ™ मैं नहीं जानता, लेकिन यह मेरे लिए अच्छी तरह से काम करता है।