我正在开发一个多客户端/服务器聊天应用程序,该应用程序可以将一个客户端的输入写入多个客户端.对于客户端来说,它运行良好,但是对于服务器端,我想添加一个部分,使其可以在自己的屏幕上打印出来自客户端的输入.当我在处理它时,我遇到init()问题,它的“ self.app = app”行恰好包含3个论点(给出2个)
这是我的代码
import kivy
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.button import Button
from kivy.support import install_twisted_reactor
install_twisted_reactor()
from twisted.internet import reactor
from twisted.internet.protocol import Protocol, Factory
class MultiClientEcho(Protocol):
def __init__(self, factory, app):
self.factory = factory
self.app = app
def connectionMade(self):
self.factory.clients.append(self)
def dataReceived(self, data):
for client in self.factory.clients:
addtolog = self.factory.app.handle_message(data)
if addtolog:
client.transport.write(data)
def connectionLost(self,reason):
self.factory.clients.remove(self)
class MultiClientEchoFactory(Factory):
protocol = MultiClientEcho
def __init__(self):
self.clients = []
def buildProtocol(self, addr):
return MultiClientEcho(self)
class ServerApp(App):
def build(self):
self.label = Label(text="server started
")
reactor.listenTCP(8000, MultiClientEchoFactory())
return self.label
def handle_message(self, msg):
self.label.text = "received: %s
" % msg
return msg
if __name__ == '__main__':
ServerApp().run()
有趣的是,我只是改编自该站点的源代码:http://kivy.org/docs/guide/other-frameworks.html,它也可以很好地运行,但是一旦将协议更改为MultiClientEcho,它立即会导致此类类型错误.我怎样才能解决这个问题?