[Twisted-Python] Advice on starting a project

Alexander Zhukov tard at 2kservices.com
Thu Aug 7 03:00:45 EDT 2003


Hello RLivsey,

Wednesday, August 6, 2003, 7:18:53 PM, you wrote:

R> How would I get a list of clients currently connected so that I can send
R> a message from one client to its recipient?
R> Any advice on what to look at and any examples that would be useful?

I'm new to twisted too and work on a similar project now (twisted
on serverside and flash and C# on clientside).

I'm not sure if it's the right way in twisted, but this example
receivs lines from one connection and writes them to all other
connections.

from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

class Chat(LineReceiver):
    def connectionMade(self):
        self.factory.addClient(self)

    def lineReceived(self,line):
        self.factory.sendAll(line)

    def connectionLost(self, reason):
        self.factory.removeClient(self)

class MyFactory(Factory):

    protocol = Chat

    def startFactory(self):
        self.clients = []

    def addClient(self,cli):
        self.clients.append(cli)

    def removeClient(self,cli):
        self.clients.remove(cli)

    def sendAll(self,msg):
        [cli.sendLine(msg) for cli in self.clients]

def main():
    f = MyFactory()
    reactor.listenTCP(8000, f)
    reactor.run()

if __name__ == '__main__':
    main()

-- 
Best regards,
 Alexander                            mailto:tard at 2kservices.com





More information about the Twisted-Python mailing list