I'm tring wampv2 and plan to use on my project. However, there's several things I do not how to handle.
I do not know how to publish to a group of clients who subscribed a topic rather than publish to all subscribed the topic.
General Question: how to publish to specific clients?
I know that autobahn.js has an API: session.publish('com.myapp.hello', ['Hello, world!'], {}, {eligible: [123, 456]});
However, how to get the client ids for the eligible array?
I did the following:
-
start crossbar.io router
-
write a nodejs client using autobahn.js
I was trying this nodejs client as a publisher.
Here's the sample code:
var autobahn = require('autobahn'),
_ = require('underscore'),
md5 = require('md5');
var coection = new autobahn.Coection({
url: 'ws://127.0.0.1:8080/ws',
realm: 'realm1'
});
var shared_secret = 'cflyhao';
var userkeyToId = {};
coection.onopen = function(session, details){
var userAuth = function(args){
var clientid = args[0];
var userkey = args[1];
var signature = args[2];
if(isValidSignature(userkey, signature)){
if(!(_.has(userkeyToId, userkey))){
userkeyToId[userkey] = {};
}
userkeyToId[userkey][clientid] = clientid;
retu true;
}else{
retu false;
}
};
var userUnAuth = function(args){
var clientid = args[0];
var userkey = args[1];
var signature = args[2];
if(isValidSignature(userkey, signature)){
if(_.has(userkeyToId, userkey)){
delete userkeyToId[userkey][clientid];
}
}
};
session.register('user.auth', userAuth);
session.register('user.unauth', userUnAuth);
};
coection.open();
function isValidSignature(userkey, signature){
retu signature == md5(userkey + shared_secret);
}
- write a browser client using autobahn.js too.
I was trying to use this browser client as a receiver.
Here's the sample code:
var usersessionid = null;
var userkey = "userkey1";
var shared_secret = "cflyhao";
var signature = md5(userkey + shared_secret);
var coection = new autobahn.Coection({
url: 'ws://127.0.0.1:8080/ws',
realm: 'realm1'
});
coection.onopen = function (session, details) {
usersessionid = session.id;
session.call('user.auth', [session.id, userkey, signature]).then(function(res){
console.log('res', res);
});
};
coection.onclose = function(reason, details){
//this is not work
//how to perform user.unauth, where to handle this
usersession.call('user.unauth', [usersessionid, userkey, signature]).then(function(res){
console.log('oncloseres', res);
});
};
coection.open();
You can see that I was tring to store a userkeyToID map in the nodejs client. The browser client will call 'user.auth' to report his clientid.
The user.auth works fine. Howerver, do not know how to handle the user.unauth, user.unauth responsible for deleting discoected clients from the map.
Specific Question: How to make user.unauth work?
Please help for the General Question and the Specific Question.
Many thanks in advance!
