【发布时间】:2015-05-02 05:05:53
【问题描述】:
我想从MyServerProtocol 类外部调用sendMessage 方法并向连接的客户端发送消息。我使用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
我的代码有什么问题?
我这样做是否正确?还是有其他方法可以从协议类之外向客户端发送消息?
谢谢。
【问题讨论】:
-
调用 send 时 self.factory 是否存在?尝试在 start() 和 send() 之间休眠并检查。另外,使用调试器。
-
你有没有想过如何做到这一点?我也有同样的问题。