Source

ginsfsm / ginsfsm / protocols / sockjs / server / transports / xhrstreaming.py

The default branch has multiple heads

Full commit
# -*- coding: utf-8 -*-
"""
    ginsfsm.protocols.sockjs.server.transports.xhrstreaming
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Xhr-Streaming transport implementation
"""
from pyramid.view import view_config

from ginsfsm.gobj import GObj
from ginsfsm.protocols.sockjs.server.transports import streamingbase

#----------------------------------------------------------------#
#                   GClass
#----------------------------------------------------------------#
GXHRSTREAMING_GCONFIG = {
    'sockjs_server': [None, None, 0, None, ""],
}


class GXhrStreaming(GObj):
    """  GStreamingBase GObj.
    """
    def __init__(self):
        GObj.__init__(self, {}, GXHRSTREAMING_GCONFIG)

    def start_up(self):
        """ Initialization zone.
        """
        # views
        """
        self.sockjs_server.pyramid_config.add_view(
            context=GXhrStreaming,
            name='',
            view=XhrStreamingTransport,
            attr='post',
            path_info=self.resource_path(),
            request_method='POST',
            permission=self.sockjs_server.permission,
        )
        """


#----------------------------------------------------------------#
#                   Info views
#----------------------------------------------------------------#
@view_config(
    context=GXhrStreaming,
    name='',
    attr='post',
    request_method='POST',
)
class XhrStreamingTransport(streamingbase.StreamingTransportBase):
    name = 'xhr_streaming'

    #def post(self, session_id):
    def post(self):
        # Handle cookie
        session_id = '?'
        response = self.response
        response.content_type = 'application/javascript; charset=UTF-8'
        self.preflight()
        self.handle_session_cookie()
        self.disable_cache()

        # Send prelude and flush any pending messages
        response.body = 'h' * 2048 + '\n'
        #self.flush()

        if not self._attach_session(session_id, False):
            self.finish()
            return

        if self.session:
            self.session.flush()

    def send_pack(self, message, binary=False):
        if binary:
            raise Exception('binary not supported for XhrStreamingTransport')

        self.active = False

        try:
            self.notify_sent(len(message))

            self.write(message + '\n')
            self.flush(callback=self.send_complete)
        except IOError:
            # If connection dropped, make sure we close offending session instead
            # of propagating error all way up.
            self.session.delayed_close()
            self._detach()