我试图执行以下操作:
我使用扭曲和高速公路来做到这一点。通过为客户端派生WebSocketClientProtocol,并在第二个派生ApplicationSession,我设法让这两个部分分开工作。两者用同一个反应堆运行。
然而,我不确定如何让他们沟通。我希望在客户端收到消息时在我的服务器上发送消息,但我不知道如何获取WebSocketClientProtocol的运行实例...
也许这也不是正确的做法。什么是正确的方法?
答案 0 :(得分:0)
我最近一直试图解决类似的问题,这里有什么用:
QUIT
^这是f = XLeagueBotFactory()
app = Application(f)
reactor.connectTCP("irc.gamesurge.net", 6667, f)
reactor.listenTCP(port, app, interface=host)
if __name__ == "__main__":
将实例定义为self,然后在我的实例中,我将其发送到另一个处理程序以获取http post请求(使用cyclone)
class Application(web.Application):
def __init__(self, botfactory):
self.botfactory = botfactory
现在重要的部分出现在工厂
class requestvouch(web.RequestHandler):
def __init__(self, application, request, **kwargs):
super(requestvouch, self).__init__(application, request, **kwargs)
self.botfactory = application.botfactory
def msg(self, channel, msg):
bot = self.botfactory.getProtocolByName("XLeagueBot")
sendmsg(bot, channel, msg) # function that processed the msg through stuff like encoding and logging and then sent it to bot.msg() function that posts it to IRC (endpoint in my case)
def post(self):
msg = "What I'm sending to the protocol of the other thing"
self.msg("#xleague", msg)
最后在我的客户端类中:
class XLeagueBotFactory(protocol.ClientFactory):
protocol = XLeagueBot
def __init__(self):
self.protocols = {}
def getProtocolByName(self, name):
return self.protocols.get(name)
def registerProtocol(self, protocol):
self.protocols[protocol.nickname] = protocol
def unregisterProtocol(self, protocol):
del self.protocols[protocol.nickname]
我不完全确定这段代码是完美的还是无错误的,但它应该+ - 告诉你如何处理调用协议类的实例。 afaik的问题来自实例协议的名称,该协议是在其工厂内部生成的,而不是发送到其他地方。