[Twisted-Python] Writing onto txWS

Corbin Simpson cds at corbinsimpson.com
Fri Jun 29 12:27:45 EDT 2012


On Fri, Jun 29, 2012 at 04:52:06PM +0200, vinod kumar wrote:
> Hi All,
> 
>    I have run and edited the example provided for txWS. The writing part
> from server to client browser is shown only as self.transport.write()
> inside dataReceived function in websockets.py. how do I write on to the
> socket i.e. on to the browser, from a different function inside server
> code(server.tac). I'm just starting another xmlrpc server on a thread where
> I receive some notifications. I need to forward the same to socket. The
> bold point below  is where I want to write on to socket. How can we do
> that? Please help me out.
> -------------------------------------------------------
> import time
> 
> from twisted.internet import reactor
> from twisted.application import internet
> from twisted.application.service import Application, Service
> import xmlrpclib
> from txws import WebSocketFactory
> 
> from src.protocols.websockets import WebSocketServerFactory
> from constants import *
> 
> class WebSocketService(Service):
>     """
>     A simple service that listens on port 8077 for WebSockets traffic.
>     """
>     def __init__(self):
>         self.start_time = time.time()
> 
>     def start_service(self, application):
>         """
>         Gets the show on the road. Fires up a factory, binds the port.
>         """
>         echofactory = WebSocketServerFactory(self)
>         factory = WebSocketFactory(echofactory)
>         ws_server = internet.TCPServer(8077, factory)
>         ws_server.setName('ws-tcp')
>         ws_server.setServiceParent(application)
> 
>     def shutdown(self):
>         """
>         Gracefully shuts down the service.
>         """
>         reactor.callLater(0, reactor.stop)
> 
> def listDevices(id):
> print "listDevice: %(id)s" % {"id": str(id)}
>  return None
> 
> def event(id, address, key, value):
>         #websocket.send("ufffffffffuuuuuu")
> *        #I need to write the below print statement on to browser *
>         print "event: %(id)s: %(address)s: %(key)s = %(value)s" % {"id":
> str(id),
> "address": str(address), "key": str(key), "value": str(value)}
> 
> def aSillyBlockingMethod(x):
>     import time
>     import xmlrpclib
>     time.sleep(2)
>     from SimpleXMLRPCServer import SimpleXMLRPCServer
>     from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
>     from constants import *
>     server = SimpleXMLRPCServer((SERVER_IP, SERVER_PORT), logRequests=True,
> allow_none=True)
>     server.register_introspection_functions()
>     server.register_multicall_functions()
>     print "server started at %s" % str(SERVER_URL)
>     server.register_function(listDevices)
>     server.register_function(event)
>     print 'function also got registered'
>     server.serve_forever()
>     .............................continues
> 
> Thanks,
> Vinodh

Hi Vinodh,

txWS merely lets you connect to a Twisted Factory through WebSockets.
You will still need to structure your code such that the events that you
are responding to are connected to that Factory.

It might be helpful to rewrite your XML-RPC server to use Twisted's
XML-RPC (see
http://twistedmatrix.com/documents/current/web/howto/xmlrpc.html for a
tutorial on that) and then it will be easier to hook up your events
without having to worry about threading or other blocking things.

If you're having specific problems with txWS, let me know; it's
important to us that txWS work smoothly for people using it.

~ C.



More information about the Twisted-Python mailing list