[Twisted-web] Writing onto txWS

vinod kumar vk.86.811 at gmail.com
Fri Jun 29 05:02:35 EDT 2012


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. The bold point below  is where I want to write on to socket. How can
we do that?
-------------------------------------------------------
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)
        ws_server.writeSomeData(self,'abrakadabra..lol')

    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: http://twistedmatrix.com/pipermail/twisted-web/attachments/20120629/89b5bbcd/attachment.htm 


More information about the Twisted-web mailing list