即時通訊使用扭曲,使一個簡單的服務器,接受多個連接,我想統計已連接的客戶端的數量。這個計數即時在工廠(使用clientConnectionMade()邏輯),但不會更新計數器的值,我真的不知道它在哪裏是我的錯誤。我讚賞一點幫助。計數器服務客戶端在一個簡單的服務器使用Python +扭曲
我的服務器代碼:(也http://bpaste.net/show/26789/)
import socket
import datetime
from twisted.internet import reactor, protocol
from twisted.internet.protocol import Factory, Protocol
class Echo(protocol.Protocol):
def connectionMade(self):
print "New client connected"
def dataReceived(self, data):
print "Msg from the client received"
if data == "datetime":
now = datetime.datetime.now()
self.transport.write("Date and time:")
self.transport.write(str(now))
elif data == "clientes":
self.transport.write("Numbers of clients served: %d " % (self.factory.numClients))
else:
self.transport.write("msg received without actions")
class EchoFactory(Factory):
protocol = Echo
def __init__(self):
self.numClients = 0
def clientConnectionMade(self):
self.numClients = self.numClients+1
def main():
factory = EchoFactory()
factory.protocol = Echo
reactor.listenTCP(9000,factory)
reactor.run()
# this only runs if the module was *not* imported
if __name__ == '__main__':
main()
犯規顯示任何錯誤,只是不更新計數器「numClients」,我不知道爲什麼。
感謝
這個編譯?你的間隔是不正確的... – Ben 2012-04-09 20:15:04