суббота, 12 сентября 2015 г., 9:58:43 UTC+6 пользователь Seyed Hadi 
Hosseini написал:
>
> hi every body
>
> is anyone of you tried to use Mojo Web Socket with Socket.IO ?
>
> Mojo is working with some Web Socket client except Socket.IO, why? if it's 
> web socket protocol, it shouldn't be like this !!
>
> anyway is any solution to get this to work? what is best client Web Socket 
> library in JS ?
>
> Thanks for your attention
>


Well my solution:
# Backend Routes in controller
sub mainPageRenderer{
    my $s=shift;
    if( !$s->session('Auth') ){
        $s->redirect_to('/p/user');
        return;
    }
    $s=$s->stash({
        title  =>'SomeApp',
        layout =>'react_SomeApp',
        override => ['/SomeApp/css/Some.css'],
        scripts   =>[
            '/common/WebSockLib.js',
            '/react/react-with-addons.js',
            '/react/JSXTransformer.js',
        ],
        jsx             =>[/someapp/jsx/some.jsx],
    }); 
    $s->render( 'some_app/main_page' );
};

sub ws_api{
    my $s=shift;
    my $wsh=App::Models::SomeModel::Service::WebSocket->new();
    $s->inactivity_timeout( 420 );
    $wsh->define_user( $wsc, $s );
    my $id=$s->tx->handshake->connection;
    my $api=App::Models::SomeModel::SomeModelAPI->new( $s->session( 'role' 
) );
    $s->on(
        message => sub{ 
            my ( $s, $r ) = (shift,shift);
            if($r && $r !~ /REF/ && $r =~ /.{2,}/){
                $r=from_json($r);
                $r->{aul}=$s->session('aul');
            }
            $wsh->handle_message( $wsc, $s, $api->api_exec( $r ) ); 
      },
        finish => sub{ 
            $wsh->drop_user( $wsc, $id );    
        }
  );
}

# Frontend Part
 var ws = new WebSocket('ws://xxx.xxx.xxx.xxx/p/ws_api');
ws.clients={};

ws.onmessage=function (msg){
  msg=JSON.parse(msg.data);
    if(!__map_exists(msg)){
      console.log('NO API METHOD IN ROUTE MAP');
    }
 return( null );
};

ws.onopen=function () {
  console.log('Connection opened');
  ws.send(JSON.stringify({p_action:'keep_alive'})); 
  setInterval( function(){KeepAlive();}, 300000);
};

function KeepAlive(){
  var m = {p_action:'keep_alive'};
  ws.send( JSON.stringify(m) );
  console.info(m);
}

function __map_exists(msg){
  var route_existance = false;
    for(var key in routemap){
      if( key === msg.listener ){
       route_existance=routemap[key](msg); 
     }
    }
    return( route_existance );
}

var routemap = {
    /* sidebar api handler */
    sup_sidebar : function(msg){
            ws.clients.common_sidebar(msg);
            return( true );
    },

    sup_formbar : function(msg){
        ws.clients.common_sidebar(msg);
        return( true );
    },

    sup_common : function(msg){
            ws.clients.r_common(msg);
    return( true );
    },

    sup_common_2 : function(msg){
            ws.clients.r_common_2(msg);
    return( true );



    },
    sup_filter : function(msg){
      ws.clients.r_filter(msg);
      return( true );
    },

    sup_stat : function(msg){
      ws.clients.r_stat(msg);
      return( true );
    },

    /* service handler used on connection and server msg*/
    all : function(msg){ return(true); }
}


# And the executable element

function ReactSidebar(){
    var Sidebar = React.createClass({
      mixins: [SetIntervalMixin], 
    Watcher:function(msg){
      this.setState({data: msg.data});
    },

    getInitialState: function(){
      return {data: []};
    },

    componentDidMount: function(){

      ws.clients.common_sidebar=this.Watcher;
      ws.send(JSON.stringify({p_action:'g_note', listener: 
'sup_sidebar'})); 
    },

-- 
You received this message because you are subscribed to the Google Groups 
"Mojolicious" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/mojolicious.
For more options, visit https://groups.google.com/d/optout.

Reply via email to