<s> from sockjs . tornado import conn , session <EOL> from sockjs . tornado . transports import base <EOL> class ChannelSession ( session . BaseSession ) : <EOL> def __init__ ( self , conn , server , base , name ) : <EOL> super ( ChannelSession , self ) . __init__ ( conn , server ) <EOL> self . base = base <EOL> self . name = name <EOL> def send_message ( self , msg , stats = True , binary = False ) : <EOL> self . base . send ( '<STR_LIT>' + self . name + '<STR_LIT:U+002C>' + msg ) <EOL> def on_message ( self , msg ) : <EOL> self . conn . on_message ( msg ) <EOL> def close ( self , code = <NUM_LIT> , message = '<STR_LIT>' ) : <EOL> self . base . send ( '<STR_LIT>' + self . name ) <EOL> self . _close ( code , message ) <EOL> def _close ( self , code = <NUM_LIT> , message = '<STR_LIT>' ) : <EOL> super ( ChannelSession , self ) . close ( code , message ) <EOL> class DummyHandler ( base . BaseTransportMixin ) : <EOL> name = '<STR_LIT>' <EOL> def __init__ ( self , conn_info ) : <EOL> self . conn_info = conn_info <EOL> def get_conn_info ( self ) : <EOL> return self . conn_info <EOL> class MultiplexConnection ( conn . SockJSConnection ) : <EOL> channels = dict ( ) <EOL> def on_open ( self , info ) : <EOL> self . endpoints = dict ( ) <EOL> self . handler = DummyHandler ( self . session . conn_info ) <EOL> def on_message ( self , msg ) : <EOL> parts = msg . split ( '<STR_LIT:U+002C>' , <NUM_LIT:2> ) <EOL> op , chan = parts [ <NUM_LIT:0> ] , parts [ <NUM_LIT:1> ] <EOL> if chan not in self . channels : <EOL> return <EOL> if chan in self . endpoints : <EOL> session = self . endpoints [ chan ] <EOL> if op == '<STR_LIT>' : <EOL> del self . endpoints [ chan ] <EOL> session . _close ( ) <EOL> elif op == '<STR_LIT>' : <EOL> session . on_message ( parts [ <NUM_LIT:2> ] ) <EOL> else : <EOL> if op == '<STR_LIT>' : <EOL> session = ChannelSession ( self . channels [ chan ] , <EOL> self . session . server , <EOL> self , <EOL> chan ) <EOL> session . set_handler ( self . handler ) <EOL> session . verify_state ( ) <EOL> self . endpoints [ chan ] = session <EOL> def on_close ( self ) : <EOL> for chan in self . endpoints : <EOL> self . endpoints [ chan ] . _close ( ) <EOL> @ classmethod <EOL> def get ( cls , ** kwargs ) : <EOL> return type ( '<STR_LIT>' , ( MultiplexConnection , ) , dict ( channels = kwargs ) ) </s>
