[You'll probably get more answers to Twisted questions on the Twisted
mailing-list:
tw************@ twistedmatrix.c om]
On Sun, Feb 29, 2004 at 02:06:07PM -0500, Qp wrote:
Hello. I'm building a simple chat server and client interface, and I've got
everything working except for this:
While the client's basic.LineRecei ver protocol class can sendLine when a
connection is made (that is, when the class is called automatically),
whenever I try to call sendLine from another class (my ClientFactory class)
then I get the following error.
"in sendLine
return self.transport. write(line + self.delimiter)
AttributeError: 'NoneType' object has no attribute 'write'"
So, I assume the instance of the LineReceiver is not set up properly. How
do I do this?
The code I am using for the classes is as follows. The error comes when I
call chatFactory.sen dMessage():
[...] class chatFactory(pro tocol.ClientFac tory):
protocol = chatClient
[...] def sendMessage(sel f):
c = chatClient()
c.sendLine("Hey there")
I see you are using ClientFactory, so I presume you want to make a client
connection to a remote server with this protocol.
You need to connect your protocol, not just create the object out of thin
air. See:
http://twistedmatrix.com/documents/howto/clients
and also:
http://twistedmatrix.com/documents/c...tml#connectTCP
I think the easiest way to do what you need is to have the protocol's
connectionMade signal the factory:
----
class ChatClient(basi c.LineReceiver) :
def connectionMade( self):
self.sendLine(" A new person has entered the room!")
self.factory.cl ientReady(self)
def lineReceived(se lf, line):
app.text_output .config(state=N ORMAL) #enable text_output for insert
app.text_output .insert(END, line +"\n")
app.text_output .config(state=D ISABLED)
app.text_input. delete(0, END) #clear text_input
app.text_output .see(END) #move scrollbar to the bottom
def connectionLost( self, reason):
reactor.stop()
class ChatFactory(pro tocol.ClientFac tory):
protocol = ChatClient
def clientConnectio nFailed(self, connector, reason):
reactor.stop()
def clientConnectio nLost(self, connector, reason):
reactor.stop()
def startFactory(se lf):
self.messageQue ue = []
self.clientInst ance = None
def clientReady(sel f, instance):
self.clientInst ance = instance
for msg in self.messageQue ue:
self.sendMessag e(msg)
def sendMessage(sel f, msg='Hey there'):
if self.clientInst ance is not None:
self.clientInst ance.sendLine(m sg)
else:
self.messageQue ue.append(msg)
----
Note that I've added a simple message queue because it may take some time
for the connection to be established, so calls to sendMessage could fail if
called too soon. The queue avoids that problem.
A better solution might be to not write the factory at all, and use
twisted.interne t.protocol.clie ntCreator:
http://twistedmatrix.com/documents/c...ntCreator.html
----
from twisted.python import log
# Create creator and connect
clientCreator = protocol.Client Creator(reactor , ChatClient)
deferred = clientCreator.c onnectTCP(host, port)
# When connected, send a line
def connectionReady (protocol):
protocol.sendLi ne('Hey there')
deferred.addCal lback(connectio nReady)
# Log any errors that occur (such as connection failed)
deferred.addErr back(log.err)
----
-Andrew.