<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: inherit;">I've got a simple little chat server set up that people can telnet into. The code is as follows:<br><br>----------<br><br>from twisted.internet import reactor<br>from twisted.internet.protocol import ServerFactory <br>from twisted.protocols.basic import LineOnlyReceiver <br>import threading<br><br>class ChatProtocol(LineOnlyReceiver): <br><br> name = "" <br><br> def getName(self): <br> if self.name!="": <br> return self.name <br> return self.transport.getPeer().host <br><br> def connectionMade(self): <br> print "New connection from "+self.getName() <br>
self.sendLine("Welcome to the chat.\r\n/quit to quit.") <br> self.factory.sendMessageToAllClients(self.getName()+" joined the chat.") <br> self.factory.clientProtocols.append(self)<br><br> def connectionLost(self, reason): <br> print "Lost connection from "+self.getName() <br> self.factory.clientProtocols.remove(self) <br> self.factory.sendMessageToAllClients(self.getName()+" disconnected.") <br><br> def lineReceived(self, line): <br> if line=="/quit": <br> self.transport.loseConnection() <br> else:
<br> self.factory.sendMessageToAllClients(self.getName()+": "+line) <br><br> def sendLine(self, line): <br> self.transport.write(line+"\r\n") <br><br>class ChatProtocolFactory(ServerFactory): <br><br> protocol = ChatProtocol <br><br> def __init__(self): <br> self.clientProtocols = [] <br><br> def sendMessageToAllClients(self, mesg): <br> for client in self.clientProtocols:<br> client.sendLine(mesg) <br> <br>def Test():<br> global test<br> factory.sendMessageToAllClients('Hey.')<br> test = threading.Timer(1.0,
Test)<br> test.start()<br>test = threading.Timer(1.0, Test)<br>test.start()<br><br>print "Starting Server"<br>factory = ChatProtocolFactory()<br>reactor.listenTCP(3009, factory)<br>reactor.run()<br><br>----------<br><br>The
problem is that last def, "def Test()". It should send the line "Hey."
to all connected users every second. It does that - however, the
message only sends through if the client is typing or has entered a
line. So, if a client is idle for 30 seconds, then starts typing, he
will receive "Hey." 30 times in a row - one for each second he was idle.<br><br>Is there any way to get this to send the message and have it show up for a client even when they're idle?</td></tr></table>