示例#1
0
    def setUp(self):
        class SavingEchoFactory(Factory):
            def buildProtocol(oself, addr):
                return self.echoProtocol

        factory = SavingEchoFactory()
        self.echoProtocol = WebSocketsProtocol(SavingEchoReceiver())

        self.resource = WebSocketsResource(lookupProtocolForFactory(factory))
示例#2
0
    def setUp(self):

        class SavingEchoFactory(Factory):

            def buildProtocol(oself, addr):
                return self.echoProtocol

        factory = SavingEchoFactory()
        self.echoProtocol = WebSocketsProtocol(SavingEchoReceiver())

        self.resource = WebSocketsResource(lookupProtocolForFactory(factory))
class ChatFactory(Factory):
protocol = MyChat
clients = []
 
resource = WebSocketsResource(lookupProtocolForFactory(ChatFactory()))
root = Resource()
#serve chat protocol on /ws
root.putChild("ws",resource)
 
application = service.Application("chatserver")
#run a TCP server on port 1025, serving the chat protocol.
internet.TCPServer(1025, Site(root)).setServiceParent(application)
示例#4
0
def run():
    factory = serverFactoryProtocol()
    reactor.listenTCP(TCP_PORT, factory)

    # Config HTTP
    from http import app
    from twisted.web.wsgi import WSGIResource
    resource = WSGIResource(reactor, reactor.getThreadPool(), app)
    reactor.listenTCP(HTTP_PORT, Site(resource))

    # Config Websocket
    ws_resource = WebSocketsResource(lookupProtocolForFactory(factory))
    reactor.listenTCP(WEBSOCKET_PORT, Site(ws_resource))

    reactor.run()
示例#5
0
            if data:
                try:
                    request.write(self.__format_response(request, 1, data.message, data.published_at))
                    request.finish()
                except:
                    print 'connection lost before complete.'
                finally:
                    self.delayed_requests.remove(request)
 
    def __format_response(self, request, status, data, timestamp=float(time.time())):
        response = json.dumps({'status':status,'timestamp': timestamp, 'data':data})
       
        if hasattr(request, 'jsonpcallback'):
            return request.jsonpcallback+'('+response+')'
        else:
            return response

from twisted.web.resource import Resource
from twisted.web.server import Site
 
from twisted.internet import protocol
from twisted.application import service, internet
 
resource = HttpChat()
factory = Site(resource)
ws_resource = WebSocketsResource(lookupProtocolForFactory(resource.wsFactory))
root = Resource()
root.putChild("",resource) #the http protocol is up at /
root.putChild("ws",ws_resource) #the websocket protocol is at /ws
application = service.Application("chatserver")
internet.TCPServer(1025, Site(root)).setServiceParent(application)
示例#6
0
        for c in self.factory.clients:
            c.message(data)

    def message(self, message):
        self.transport.write(message + '\n')

from twisted.web.resource import Resource
from twisted.web.server import Site
from twisted.internet import protocol
from twisted.application import service, internet

# Create a protocol factory
# The factory is usually a singleton, and
# all instantiated protocols should have a reference to it,
# so we'll use it to store shared state
#( the list of currently connected clients
from twisted.internet.protocol import Factory
class ChatFactory(Factory):
    protocol = MyChat
    clients = []

resource = WebSocketsResource(lookupProtocolForFactory(ChatFactory()))
root = Resource()
# serve chat protocol on /ws
root.putChild("ws", resource)

application = service.Application("chatserver")
# run a TCP server on port 1025, serving the chat protocol.
internet.TCPServer(1025, Site(root)).setServiceParent(application)

示例#7
0
            if coo[0] == "x":
                self.p.do_pan(int(coo[1]))
                print "Update X=%s" % coo[1]
            if coo[0] == "y":
                self.p.do_tilt(int(coo[1]))
                print "Update Y=%s" % coo[1]
        self.factory.messages[float(time.time())] = data
        self.updateClients(data)

    def updateClients(self, data):
        for c in self.factory.clients:
            c.message(data)

    def message(self, message):
        self.transport.write(message + '\n')


class ChatFactory(Factory):
    protocol = WebsocketChat
    clients = []
    messages = {}


resource = WebSocketsResource(lookupProtocolForFactory(ChatFactory()))
root = Resource()
#serve chat protocol on /ws
root.putChild("ws", resource)
application = service.Application("chatserver")
#run a TCP server on port 1025, serving the chat protocol.
internet.TCPServer(1025, Site(root)).setServiceParent(application)
示例#8
0
from twisted_share.wsgi import WsgiRoot
from twisted.python.threadpool import ThreadPool

from django.core.handlers.wsgi import WSGIHandler


# Environment setup for your Django project files:
#sys.path.append("cib_simulator")
sys.path.append("gameMT")
os.environ['DJANGO_SETTINGS_MODULE'] = 'gameMT.settings'

shared_messages = {}

resource = HttpShare(shared_messages)
factory = Site(resource)
ws_resource = WebSocketsResource(lookupProtocolForFactory(resource.wsFactory))

#Create a resource which will correspond to the root of the URL hierarchy: all URLs are children of this resource.
root = Resource()
root.putChild("",resource) #the http protocol is up at /
root.putChild("ws",ws_resource) #the websocket protocol is at /ws

# Twisted Application Framework setup:
application = service.Application("shareserver")

#This is the port for pass messages
internet.TCPServer(1035, Site(root)).setServiceParent(application)

#serving django over wsgi
# Create and start a thread pool,
wsgiThreadPool = ThreadPool()
 def getChild(self, name, request):
     factory = self.factories.get(name)
     if not factory:
         factory = self.wsFactory(name)
         self.factories[name] = factory
     return WebSocketsResource(lookupProtocolForFactory(factory))