# Path: sockjs/transports/eventsource.py
# class EventsourceTransport(StreamingTransport):
#     async def send(self, text):
#         blob = "".join(("data: ", text, "\r\n\r\n")).encode(ENCODING)
#         await self.response.write(blob)
# 
#         self.size += len(blob)
#         if self.size > self.maxsize:
#             return True
#         else:
#             return False
# 
#     async def process(self):
#         headers = (
#             (hdrs.CONTENT_TYPE, "text/event-stream"),
#             (hdrs.CACHE_CONTROL, CACHE_CONTROL),
#         )
#         headers += session_cookie(self.request)
# 
#         # open sequence (sockjs protocol)
#         resp = self.response = web.StreamResponse(headers=headers)
#         await resp.prepare(self.request)
#         await resp.write(b"\r\n")
# 
#         # handle session
#         await self.handle_session()
# 
#         return resp
# 
# Path: sockjs/transports/htmlfile.py
# class HTMLFileTransport(StreamingTransport):
# 
#     maxsize = 131072  # 128K bytes
#     check_callback = re.compile(r"^[a-zA-Z0-9_\.]+$")
# 
#     async def send(self, text):
#         blob = ("<script>\np(%s);\n</script>\r\n" % dumps(text)).encode(ENCODING)
#         await self.response.write(blob)
# 
#         self.size += len(blob)
#         if self.size > self.maxsize:
#             return True
#         else:
#             return False
# 
#     async def process(self):
#         request = self.request
# 
#         try:
#             callback = request.query.get("c", None)
#         except Exception:
#             callback = request.GET.get("c", None)
# 
#         if callback is None:
#             await self.session._remote_closed()
#             return web.HTTPInternalServerError(text='"callback" parameter required')
# 
#         elif not self.check_callback.match(callback):
#             await self.session._remote_closed()
#             return web.HTTPInternalServerError(text='invalid "callback" parameter')
# 
#         headers = (
#             (hdrs.CONTENT_TYPE, "text/html; charset=UTF-8"),
#             (hdrs.CACHE_CONTROL, CACHE_CONTROL),
#             (hdrs.CONNECTION, "close"),
#         )
#         headers += session_cookie(request)
#         headers += cors_headers(request.headers)
# 
#         # open sequence (sockjs protocol)
#         resp = self.response = web.StreamResponse(headers=headers)
#         await resp.prepare(self.request)
#         await resp.write(
#             b"".join((PRELUDE1, callback.encode("utf-8"), PRELUDE2, b" " * 1024))
#         )
# 
#         # handle session
#         await self.handle_session()
# 
#         return resp
# 
# Path: sockjs/transports/xhrstreaming.py
# class XHRStreamingTransport(StreamingTransport):
# 
#     maxsize = 131072  # 128K bytes
#     open_seq = b"h" * 2048 + b"\n"
# 
#     async def process(self):
#         request = self.request
#         headers = (
#             (hdrs.CONNECTION, request.headers.get(hdrs.CONNECTION, "close")),
#             (hdrs.CONTENT_TYPE, "application/javascript; charset=UTF-8"),
#             (hdrs.CACHE_CONTROL, CACHE_CONTROL),
#         )
# 
#         headers += session_cookie(request)
#         headers += cors_headers(request.headers)
# 
#         if request.method == hdrs.METH_OPTIONS:
#             headers += ((hdrs.ACCESS_CONTROL_ALLOW_METHODS, "OPTIONS, POST"),)
#             headers += cache_headers()
#             return web.Response(status=204, headers=headers)
# 
#         # open sequence (sockjs protocol)
#         resp = self.response = web.StreamResponse(headers=headers)
#         resp.force_close()
#         await resp.prepare(request)
#         await resp.write(self.open_seq)
# 
#         # event loop
#         await self.handle_session()
# 
#         return resp


import asyncio
import logging
import sockjs
from aiohttp import web
from sockjs.transports.eventsource import EventsourceTransport
from sockjs.transports.htmlfile import HTMLFileTransport
from sockjs.transports.xhrstreaming import XHRStreamingTransport



async def echoSession(msg, session):
    if msg.type == sockjs.MSG_MESSAGE:
        session.send(msg.data)


async def closeSessionHander(msg, session):
    if msg.type == sockjs.MSG_OPEN:
        session.close()


async def broadcastSession(msg, session):
    if msg.type == sockjs.MSG_OPEN:
        session.manager.broadcast(msg.data)


if __name__ == '__main__':
    """ Sockjs tests server """
    loop = asyncio.get_event_loop()
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(levelname)s %(message)s')

    HTMLFileTransport.maxsize = 4096
    EventsourceTransport.maxsize = 4096

# Path: sockjs/transports/eventsource.py
# class EventsourceTransport(StreamingTransport):
#     async def send(self, text):
#         blob = "".join(("data: ", text, "\r\n\r\n")).encode(ENCODING)
#         await self.response.write(blob)
# 
#         self.size += len(blob)
#         if self.size > self.maxsize:
#             return True
#         else:
#             return False
# 
#     async def process(self):
#         headers = (
#             (hdrs.CONTENT_TYPE, "text/event-stream"),
#             (hdrs.CACHE_CONTROL, CACHE_CONTROL),
#         )
#         headers += session_cookie(self.request)
# 
#         # open sequence (sockjs protocol)
#         resp = self.response = web.StreamResponse(headers=headers)
#         await resp.prepare(self.request)
#         await resp.write(b"\r\n")
# 
#         # handle session
#         await self.handle_session()
# 
#         return resp
# 
# Path: sockjs/transports/htmlfile.py
# class HTMLFileTransport(StreamingTransport):
# 
#     maxsize = 131072  # 128K bytes
#     check_callback = re.compile(r"^[a-zA-Z0-9_\.]+$")
# 
#     async def send(self, text):
#         blob = ("<script>\np(%s);\n</script>\r\n" % dumps(text)).encode(ENCODING)
#         await self.response.write(blob)
# 
#         self.size += len(blob)
#         if self.size > self.maxsize:
#             return True
#         else:
#             return False
# 
#     async def process(self):
#         request = self.request
# 
#         try:
#             callback = request.query.get("c", None)
#         except Exception:
#             callback = request.GET.get("c", None)
# 
#         if callback is None:
#             await self.session._remote_closed()
#             return web.HTTPInternalServerError(text='"callback" parameter required')
# 
#         elif not self.check_callback.match(callback):
#             await self.session._remote_closed()
#             return web.HTTPInternalServerError(text='invalid "callback" parameter')
# 
#         headers = (
#             (hdrs.CONTENT_TYPE, "text/html; charset=UTF-8"),
#             (hdrs.CACHE_CONTROL, CACHE_CONTROL),
#             (hdrs.CONNECTION, "close"),
#         )
#         headers += session_cookie(request)
#         headers += cors_headers(request.headers)
# 
#         # open sequence (sockjs protocol)
#         resp = self.response = web.StreamResponse(headers=headers)
#         await resp.prepare(self.request)
#         await resp.write(
#             b"".join((PRELUDE1, callback.encode("utf-8"), PRELUDE2, b" " * 1024))
#         )
# 
#         # handle session
#         await self.handle_session()
# 
#         return resp
# 
# Path: sockjs/transports/xhrstreaming.py
# class XHRStreamingTransport(StreamingTransport):
# 
#     maxsize = 131072  # 128K bytes
#     open_seq = b"h" * 2048 + b"\n"
# 
#     async def process(self):
#         request = self.request
#         headers = (
#             (hdrs.CONNECTION, request.headers.get(hdrs.CONNECTION, "close")),
#             (hdrs.CONTENT_TYPE, "application/javascript; charset=UTF-8"),
#             (hdrs.CACHE_CONTROL, CACHE_CONTROL),
#         )
# 
#         headers += session_cookie(request)
#         headers += cors_headers(request.headers)
# 
#         if request.method == hdrs.METH_OPTIONS:
#             headers += ((hdrs.ACCESS_CONTROL_ALLOW_METHODS, "OPTIONS, POST"),)
#             headers += cache_headers()
#             return web.Response(status=204, headers=headers)
# 
#         # open sequence (sockjs protocol)
#         resp = self.response = web.StreamResponse(headers=headers)
#         resp.force_close()
#         await resp.prepare(request)
#         await resp.write(self.open_seq)
# 
#         # event loop
#         await self.handle_session()
# 
#         return resp


# Path: sockjs-testsrv.py
import asyncio
import logging
import sockjs
from aiohttp import web
from sockjs.transports.eventsource import EventsourceTransport
from sockjs.transports.htmlfile import HTMLFileTransport
from sockjs.transports.xhrstreaming import XHRStreamingTransport




async def echoSession(msg, session):
    if msg.type == sockjs.MSG_MESSAGE:
        session.send(msg.data)


async def closeSessionHander(msg, session):
    if msg.type == sockjs.MSG_OPEN:
        session.close()


async def broadcastSession(msg, session):
    if msg.type == sockjs.MSG_OPEN:
        session.manager.broadcast(msg.data)


if __name__ == '__main__':
    """ Sockjs tests server """
    loop = asyncio.get_event_loop()
    logging.basicConfig(level=logging.DEBUG,
                        format='%(asctime)s %(levelname)s %(message)s')

    HTMLFileTransport.maxsize = 4096
    EventsourceTransport.maxsize = 4096

    XHRStreamingTransport.maxsize = 4096