- 论坛徽章:
- 0
|
from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineOnlyReceiver
from twisted.internet import reactor
class Chat(LineOnlyReceiver):
def lineReceived(self,data):
self.factory.sendAll("%s :%s" % (self.getId(), data))
def getId(self):
return str(self.transport.getPeer())
def connectionMade(self):
print "New commectioin from",self.getId()
self.transport.write("Welcome to the chat server , %s\n" % self.getId())
self.factory.addClient(self)
def connectionLost(self,reason):
self.factory.delClient(self)
class ChatFactory(Factory):
protocol = Chat
def __init__(self):
self.clients = []
def addClient(self,newclient):
self.clients.append(newclient)
def delClient(self, message):
for proto in self.clients:
proto.transport.write(message + "\n")
reactor.listenTCP(5000, ChatFactory())
reactor.run()
____________
以上代码用为服务端运行后为什么sendAll不能正常工作,总是报错,或者哪位能帮我编个跟他配套的client |
|