我正在使用带有以下代码的 autobhan websockets 库:
from twisted.internet import reactor
from autobahn.websocket import WebSocketServerFactory, \
WebSocketServerProtocol, \
listenWS
class EchoServerProtocol(WebSocketServerProtocol):
def onMessage(self, msg, binary):
self.sendMessage(msg, binary)
def sendRandomMessage(message):
self.sendMessage(message,true)
if __name__ == '__main__':
factory = WebSocketServerFactory("ws://192.168.1.135:9999", debug = False)
factory.protocol = EchoServerProtocol
listenWS(factory)
reactor.run()
#insert random code here. Things happen independent of the connection state.
#somehow access EchoServerProtocolINSTANCE.sendMessage("MESSAGE_HERE")
该代码适用于接收消息,但我如何访问 EchoServerProtocol 的实例以便随时发送任意消息?