Passing additional arguments to endpoints

#1

Hello,

I really love Autobahn and Crossbar. I was just in the process of re-factoring my code when I stumbled over a little problem that prevents me from making my class as neat as possible.
Is there a way to add an additional set of arguments to the registration endpoint other than the ones specified by the caller?

You will find in my example below that I am having to declare functions acting as a bridge to be able to pass an object reference to my prototype methods. It works, but is there something obvious that I am missing that does not require dropping the use of prototype to add methods to my class?

Example:

session.register('com.base.requestUserCount', on_requestUserCount)

`var on_requestUserCount = function(args){
_this.requestUserCount(args, _this);
return 1;
};

  1. Core.prototype.requestUserCount = function(args, _this){
    _this.rclient.get(“base:spectatorCount”, function(error ,reply){
    _this.session.publish(‘com.base.broadcastUserCount’,[reply],{},{eligible: [args[0]]});
    console.log(reply);
    });
    };
    `
    Any help would be greatly appreciated.

Kind Regards,
Reda

var autobahn = require(‘autobahn’);
var redis = require(‘redis’);

/**

  • @class
  • @classdesc Core crossbar/autobahn services class
  • @param {string} HOST - Example “ws://localhost:8080/ws”
  • @param {string} REDIS_HOST - Example “127.0.0.1”
  • @param {string} REDIS_PORT - Example “1234”
    */

var Core = function(HOST,REDIS_HOST,REDIS_PORT){
/** @constructor */
this._self = this;
this.session = null;
this.connection = new autobahn.Connection({url: HOST,realm: ‘realm1’});
this.rclient = redis.createClient(REDIS_PORT, REDIS_HOST);
this.rclient.flushall();
return 1;
};

Core.prototype.registerSpectator = function(_this){
var multi = _this.rclient.multi();
multi.incr(“base:spectatorCount”);
multi.exec(function(error, reply){
_this.broadcastUserCount(_this, reply);
});
return 1;
};

Core.prototype.unregisterSpectator = function(_this){
_this.rclient.get(“base:spectatorCount”, function(error, reply) {
/* Check for a zombie connection in case of server restart */
if (reply <= 0){
_this.rclient.incr(“base:spectatorCount”);
var multi = _this.rclient.multi();
multi.decr(“base:spectatorCount”);
multi.exec(function(error, reply){
_this.broadcastUserCount(_this, reply);
});
}
});

return 1;

};

Core.prototype.broadcastUserCount = function(_this, reply){
_this.session.publish(‘com.base.broadcastUserCount’,[reply]);
};

Core.prototype.requestUserCount = function(args, _this){
_this.rclient.get(“base:spectatorCount”, function(error ,reply){
_this.session.publish(‘com.base.broadcastUserCount’,[reply],{},{eligible: [args[0]]});
console.log(reply);
});
};

Core.prototype.ping = function(){
return 1;
};

Core.prototype.start = function(){
var _this = this;

var on_event_join = function(){                    
        _this.registerSpectator(_this._self);    
        return 1;
    };
   
var on_event_leave = function(){
        _this.unregisterSpectator(_this);
        return 1;
    };
   
var on_requestUserCount = function(args){
        _this.requestUserCount(args, _this);            
        return 1;
    };


this.connection.onopen = function(session){
    _this._self.session = session;
   
    session.register('com.base.ping', _this.ping).then(
        function(registration){
            console.log('Procedure registered', registration.procedure);    
        },
        function(error){
            console.log('Registration failed', error);
        }
    );
   
    session.register('com.base.requestUserCount', on_requestUserCount).then(
        function(registration){
            console.log('Procedure registered', registration.procedure);    
        },
        function(error){
            console.log('Registration failed', error);
        }
    );
   
    session.subscribe('wamp.metaevent.session.on_join', on_event_join).then(
        function(subscription){
            console.log('Subscription succeeded', subscription.topic);
        },
        function(error){
            console.log('Subscription failed', error);
        }
    );        
   
    session.subscribe('wamp.metaevent.session.on_leave', on_event_leave).then(
        function(subscription){
            console.log('Subscription succeeded', subscription.topic);
        },
        function(error){
            console.log('Subscription failed', error);
        }
    );        
};
this.connection.open();    
return 1;

};

``

0 Likes