# Path: sockjs/transports/base.py
# class Transport:
#     def __init__(self, manager, session, request):
#         self.manager = manager
#         self.session = session
#         self.request = request
# 
# Path: sockjs/exceptions.py
# class SessionIsClosed(SockjsException):
#     """Session is closed."""
# 
# Path: sockjs/protocol.py
# STATE_CLOSED = 3
# 
# FRAME_CLOSE = "c"
# 
# Path: sockjs/protocol.py
# ENCODING = "utf-8"
# STATE_NEW = 0
# STATE_OPEN = 1
# STATE_CLOSING = 2
# STATE_CLOSED = 3
# FRAME_OPEN = "o"
# FRAME_CLOSE = "c"
# FRAME_MESSAGE = "a"
# FRAME_MESSAGE_BLOB = "a1"
# FRAME_HEARTBEAT = "h"
# IFRAME_HTML = """<!DOCTYPE html>
# <html>
# <head>
# <meta http-equiv="X-UA-Compatible" content="IE=edge" />
# <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
#   <script src="%s"></script>
#   <script>
#     document.domain = document.domain;
#     SockJS.bootstrap_iframe();
#   </script>
# </head>
# <body>
#   <h2>Don't panic!</h2>
#   <p>This is a SockJS hidden iframe. It's used for cross domain magic.</p>
# </body>
# </html>
# """.strip()
# IFRAME_MD5 = hashlib.md5(IFRAME_HTML.encode()).hexdigest()
# ENCODING = "utf-8"
# MSG_OPEN = 1
# MSG_MESSAGE = 2
# MSG_CLOSE = 3
# MSG_CLOSED = 4
#     def dthandler(obj):
# def dumps(data):
# def close_frame(code, reason):
# def message_frame(message):
# def messages_frame(messages):
#     def tp(self):
# class SockjsMessage(collections.namedtuple("SockjsMessage", ["type", "data"])):


import asyncio
from aiohttp import web
from asyncio import ensure_future
from .base import Transport
from ..exceptions import SessionIsClosed
from ..protocol import STATE_CLOSED, FRAME_CLOSE
from ..protocol import loads, close_frame

                try:
                    text = loads(data)
                except Exception as exc:
                    await session._remote_close(exc)
                    await session._remote_closed()
                    await ws.close(message=b"broken json")
                    break

                if data.startswith("["):
                    await session._remote_messages(text)
                else:
                    await session._remote_message(text)

            elif msg.type == web.WSMsgType.close:
                await session._remote_close()
            elif msg.type in (web.WSMsgType.closed, web.WSMsgType.closing):
                await session._remote_closed()
                break

    async def process(self):
        # start websocket connection
        ws = self.ws = web.WebSocketResponse()
        await ws.prepare(self.request)

        # session was interrupted
        if self.session.interrupted:
            await self.ws.send_str(close_frame(1002, "Connection interrupted"))

        elif self.session.state == STATE_CLOSED:

# Path: sockjs/transports/base.py
# class Transport:
#     def __init__(self, manager, session, request):
#         self.manager = manager
#         self.session = session
#         self.request = request
# 
# Path: sockjs/exceptions.py
# class SessionIsClosed(SockjsException):
#     """Session is closed."""
# 
# Path: sockjs/protocol.py
# STATE_CLOSED = 3
# 
# FRAME_CLOSE = "c"
# 
# Path: sockjs/protocol.py
# ENCODING = "utf-8"
# STATE_NEW = 0
# STATE_OPEN = 1
# STATE_CLOSING = 2
# STATE_CLOSED = 3
# FRAME_OPEN = "o"
# FRAME_CLOSE = "c"
# FRAME_MESSAGE = "a"
# FRAME_MESSAGE_BLOB = "a1"
# FRAME_HEARTBEAT = "h"
# IFRAME_HTML = """<!DOCTYPE html>
# <html>
# <head>
# <meta http-equiv="X-UA-Compatible" content="IE=edge" />
# <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
#   <script src="%s"></script>
#   <script>
#     document.domain = document.domain;
#     SockJS.bootstrap_iframe();
#   </script>
# </head>
# <body>
#   <h2>Don't panic!</h2>
#   <p>This is a SockJS hidden iframe. It's used for cross domain magic.</p>
# </body>
# </html>
# """.strip()
# IFRAME_MD5 = hashlib.md5(IFRAME_HTML.encode()).hexdigest()
# ENCODING = "utf-8"
# MSG_OPEN = 1
# MSG_MESSAGE = 2
# MSG_CLOSE = 3
# MSG_CLOSED = 4
#     def dthandler(obj):
# def dumps(data):
# def close_frame(code, reason):
# def message_frame(message):
# def messages_frame(messages):
#     def tp(self):
# class SockjsMessage(collections.namedtuple("SockjsMessage", ["type", "data"])):


# Path: sockjs/transports/websocket.py
import asyncio
from aiohttp import web
from asyncio import ensure_future
from .base import Transport
from ..exceptions import SessionIsClosed
from ..protocol import STATE_CLOSED, FRAME_CLOSE
from ..protocol import loads, close_frame


                try:
                    text = loads(data)
                except Exception as exc:
                    await session._remote_close(exc)
                    await session._remote_closed()
                    await ws.close(message=b"broken json")
                    break

                if data.startswith("["):
                    await session._remote_messages(text)
                else:
                    await session._remote_message(text)

            elif msg.type == web.WSMsgType.close:
                await session._remote_close()
            elif msg.type in (web.WSMsgType.closed, web.WSMsgType.closing):
                await session._remote_closed()
                break

    async def process(self):
        # start websocket connection
        ws = self.ws = web.WebSocketResponse()
        await ws.prepare(self.request)

        # session was interrupted
        if self.session.interrupted:
            await self.ws.send_str(close_frame(1002, "Connection interrupted"))

        elif self.session.state == STATE_CLOSED:

            await self.ws.send_str(close_frame(3000, "Go away!"))