I have an echoserver in Twisted that needs to get input from an echoclient. The echocient is a GUI (Panda3D). The client just sends a short message when a button is clicked.
So I have messages to send at irregular times (only when button is clicked).
How can I have a permanent connection (the reactor.run() is already started at the beginning of the client program) and send messages.
I don't want to write a polling mechanism in EchoClient/connectionMade. I saw an example of gtk+ but cannot translate it to Panda. How to go about. The code hereunder is not working at all but gives you an idea of what I want (basically permanent connection and once in a while the user sends something when pressing a button).
from direct.showbase.ShowBase import ShowBase
from direct.gui.DirectButton import DirectButton
from panda3d.core import Vec3
from direct.task import Task
from twisted.internet import protocol, reactor, defer
from twisted.internet.task import LoopingCall
from twisted.spread import pb
FRAMERATE = 32
class LoginDialog:
def __init__(self, deferred):
self.deferredResult = deferred
class EchoClient(ShowBase, protocol.Protocol):
def __init__(self):
ShowBase.__init__(self)
self.echoer = echoer
self.button = DirectButton(pos = Vec3(.1,0,.1), text = "Send request",
scale = .1, pad = (.5, .5),
rolloverSound = None, clickSound = None,
command = self.Request)
def Request():
self.echoer.transport.write("Message from client")
def dataReceived(self, data):
print "Server said: ", data
cf = pb.PBClientFactory()
cf.getRootObject().addCallback(EchoClient)
reactor.connectTCP("localhost", 17000, cf)
LoopingCall(taskMgr.step).start(1 / FRAMERATE)
reactor.run()