[Twisted-Python] txWS !!

vinod kumar vk.86.811 at gmail.com
Sat Jun 30 13:36:08 MDT 2012


Hi,

 Thanks a lot for your reply. The trouble is that the events I have are
triggered by a homematic CCU. The CCU does it by multicall which has not
yet been implemented in twisted servers. So, I'm running a non-twisted
xmlrpc server in a thread so that my CCU triggers the events and it's
happening too. I can see the events printed on the console too. Now, I just
want a way to write the same to sockets. Can't we connect these to txWS
factory ? Doesn't the txWS has some socket object so that we can just say
socket.write..hmm

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.
-------------------------------
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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20120630/f5420b3a/attachment.html>


More information about the Twisted-Python mailing list