import asyncio
import mimetypes
import os
import time

from aiohttp import Response, EofStream
from aiohttp.server import ServerHttpProtocol
from aiohttp.websocket import do_handshake, MSG_PING, MSG_TEXT, MSG_CLOSE


class HttpServer(ServerHttpProtocol):

    @asyncio.coroutine
    def handle_request(self, message, payload):
        now = time.time()
        upgrade = False
        for hdr, val in message.headers:
            if hdr == 'UPGRADE':
                upgrade = 'websocket' in val.lower()
                break

        if upgrade:
            # websocket handshake
            status, headers, parser, writer = websocket.do_handshake(
                message.method, message.headers, self.transport)

            resp = Response(self.transport, status)
            resp.add_headers(*headers)
            resp.send_headers()

            # install websocket parser
            dataqueue = self.stream.reader.set_parser(parser)

            while True:
                try:
                    msg = yield from dataqueue.read()
                except EofStream:
                    # client droped connection
                    break

                if msg.tp == MSG_PING:
                    writer.pong()

                elif msg.tp == MSG_TEXT:
                    data = msg.data.strip()
                    print('%s' % data)

                elif msg.tp == MSG_CLOSE:
                    break
        else:
            # Serve static files
            response = Response(self.transport, 200)
            response.add_header('Transfer-Encoding', 'chunked')


            path = message.path.lstrip('/') or 'index.html'
            content_type, _ = mimetypes.guess_type(path)

            response.add_header('Content-type', content_type or 'text/html')
            response.send_headers()

            try:
                with open(os.path.join('static', path), 'rb') as fp:
                    chunk = fp.read(8196)
                    while chunk:
                        if not response.write(chunk):
                            break
                        chunk = fp.read(8196)
            except OSError:
                drain = super().handle_request(message, payload)
            else:
                drain = response.write_eof()
                if response.keep_alive():
                    self.keep_alive(True)
                self.log_access(message, None, response, time.time() - now)
            return drain


def main():
    """Main function for running the server from the command line."""
    loop = asyncio.get_event_loop()
    factory = loop.create_server(lambda: HttpServer(debug=True, keep_alive=75), '127.0.0.1', 8000)
    server = loop.run_until_complete(factory)
    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass


if __name__ == '__main__':
    main()

@@ -210,16 +210,686 @@
 CLOSE%0A%0A%0A
+class WebClient(object):%0A    %22%22%22Encapsulation of client logic.%22%22%22%0A%0A    def __init__(self, loop, reader, writer):%0A        self.loop = loop%0A        self.reader = reader%0A        self.writer = writer%0A%0A    @asyncio.coroutine%0A    def run(self):%0A        while True:%0A            try:%0A                msg = yield from self.reader.read()%0A            except EofStream:%0A                # client droped connection%0A                break%0A%0A            if msg.tp == MSG_PING:%0A                self.writer.pong()%0A%0A            elif msg.tp == MSG_TEXT:%0A                data = msg.data.strip()%0A                print('%25s' %25 data)%0A%0A            elif msg.tp == MSG_CLOSE:%0A                break%0A%0A%0A
 class Ht
@@ -918,16 +918,16 @@
 tocol):%0A
-
 %0A    @as
@@ -1294,18 +1294,8 @@
 r = 
-websocket.
 do_h
@@ -1545,25 +1545,22 @@
         
-dataqueue
+reader
  = self.
@@ -1566,23 +1566,16 @@
 .stream.
-reader.
 set_pars
@@ -1585,17 +1585,16 @@
 parser)%0A
-%0A
         
@@ -1601,469 +1601,90 @@
     
-while True:%0A                try:%0A                    msg = yield from dataqueue.read()%0A                except EofStream:%0A                    # client droped connection%0A                    break%0A%0A                if msg.tp == MSG_PING:%0A                    writer.pong()%0A%0A                elif msg.tp == MSG_TEXT:%0A                    data = msg.data.strip()%0A                    print('%25s' %25 data)%0A%0A                elif msg.tp == MSG_CLOSE:%0A                    break
+client = WebClient(self._loop, reader, writer)%0A            yield from client.run()
 %0A   

Refactor websocket handling and some fixes.