I know that this is a duplicate Send multiple messages to server from twisted client, however the answers there don't really help and I still have questions on how to solve this issue.
To test, I wrote a simple echo server that just echos whatever message it receives back to the client, and for my client code I have:
class InsertRecord(Protocol):
def __init__(self, message):
print("Protocol initialized")
self.msg = message
def connectionMade(self):
self.transport.write(self.msg)
def sendMessage(self, msg):
self.transport.write("MESSAGE %s\n" % msg)
def dataReceived(self, data):
print("data")
reactor.stop()
class ClientInsertFactory(ClientFactory):
def __init__(self, dataRecord):
self.dataRecord = dataRecord
print("Client factor init")
def buildProtocol(self, addr):
return InsertRecord(self.dataRecord)
def startedConnecting(self, connector):
print("Started to connect to IS Server")
reactor.connectTCP("localhost", 2222, ClientInsertFactory("message1"))
reactor.run()
connector = reactor.connectTCP("localhost", 2222, ClientInsertFactory("message2"))
reactor.run()
I know that I am unable to stop and restart the reactor, however, I can't think of a way to get around this. What I want is to be able to generate multiple sessions with the server without getting blocked by the call to reactor.run(). As per the post linked before, I want to be able have a way to be able to call sendData outside the protocol.