Esempio n. 1
0
    def handle_request(self, message, payload):
        if message.path == '/socket':
            status, headers, parser, writer = websocket.do_handshake(
                message, self.transport)

            response = tulip.http.Response(self.transport, status)
            response.add_headers(*headers)
            response.send_headers()
            databuffer = self.stream.set_parser(parser)

            client = self.app.client_class(writer)
            yield from self.app.handle('connection_made', client, None)

            while True:
                message = yield from databuffer.read()

                if message is None or message.tp == websocket.MSG_CLOSE:
                    # You cannot use yield from here because this coroutine is
                    # about to be cancelled.
                    handle_conn_lost = tulip.task(
                        lambda: self.app.handle('connection_lost', client, None))
                    handle_conn_lost()
                    break
                elif message.tp == websocket.MSG_PING:
                    writer.pong()
                elif message.tp == websocket.MSG_TEXT:
                    data = json.loads(message.data)
                    yield from self.app.handle(data['route'], client,
                        data['content'])
        else:
            response = tulip.http.Response(self.transport, 200)
            path = urlparse(message.path).path

            mtype, encoding = mimetypes.guess_type(path)
            response.add_header('Content-type', mtype or 'text/html')
            response.send_headers()

            if path == '/':
                result = yield from self.app.get_index_html()
            elif path == '/viol.js':
                result = get_viol_js()
            elif path == '/jquery.js':
                result = JQUERY_JS
            elif path == '/favicon.ico':
                result = FAVICON
            else:
                result = get_file(path[1:])
            response.write(result)

        response.write_eof()
        self.keep_alive(False)
Esempio n. 2
0
    def handle_request(self, message, payload):
        if message.path == '/socket':
            status, headers, parser, writer = websocket.do_handshake(
                message, self.transport)

            response = tulip.http.Response(self.transport, status)
            response.add_headers(*headers)
            response.send_headers()
            databuffer = self.stream.set_parser(parser)

            client = self.app.client_class(writer)
            yield from self.app.handle('connection_made', client, None)

            while True:
                message = yield from databuffer.read()

                if message is None or message.tp == websocket.MSG_CLOSE:
                    # You cannot use yield from here because this coroutine is
                    # about to be cancelled.
                    handle_conn_lost = tulip.task(lambda: self.app.handle(
                        'connection_lost', client, None))
                    handle_conn_lost()
                    break
                elif message.tp == websocket.MSG_PING:
                    writer.pong()
                elif message.tp == websocket.MSG_TEXT:
                    data = json.loads(message.data)
                    yield from self.app.handle(data['route'], client,
                                               data['content'])
        else:
            response = tulip.http.Response(self.transport, 200)
            path = urlparse(message.path).path

            mtype, encoding = mimetypes.guess_type(path)
            response.add_header('Content-type', mtype or 'text/html')
            response.send_headers()

            if path == '/':
                result = yield from self.app.get_index_html()
            elif path == '/viol.js':
                result = get_viol_js()
            elif path == '/jquery.js':
                result = JQUERY_JS
            elif path == '/favicon.ico':
                result = FAVICON
            else:
                result = get_file(path[1:])
            response.write(result)

        response.write_eof()
        self.keep_alive(False)
Esempio n. 3
0
 def add_connection(self, route, fn):
     fn = tulip.task(fn)
     self.connections[route] = fn
     return fn
Esempio n. 4
0
 def add_connection(self, route, fn):
     fn = tulip.task(fn)
     self.connections[route] = fn
     return fn
Esempio n. 5
0
def serve(ws_handler, host=None, port=None, *,
          protocols=(), extensions=(), klass=WebSocketServerProtocol, **kwds):
    """
    This task starts a WebSocket server.

    It's a thin wrapper around the event loop's ``start_serving`` method.

    `ws_handler` is the WebSocket handler. It must be a coroutine accepting
    two arguments: a :class:`~websockets.framing.WebSocketServerProtocol` and
    the request URI. The `host` and `port` arguments and other keyword
    arguments are passed to ``start_serving``. The return value is a list of
    objects that can be passed to ``stop_serving``.

    Whenever a client connects, the server accepts the connection, creates a
    :class:`~websockets.framing.WebSocketServerProtocol`, performs the opening
    handshake, and delegates to the WebSocket handler. Once the handler
    completes, the server performs the closing handshake and closes the
    connection.
    """
    assert not protocols, "protocols aren't supported"
    assert not extensions, "extensions aren't supported"

    return (yield from tulip.get_event_loop().start_serving(
            lambda: klass(ws_handler), host, port, **kwds))


# Workaround for http://code.google.com/p/tulip/issues/detail?id=30
__serve_doc__ = serve.__doc__
serve = tulip.task(serve)
serve.__doc__ = __serve_doc__