I\'m using node-xmpp server as a chat server for 2 clients:Psi and Spark.I register an account for every client.I want to send from Psi account a message to Spark account.Ev
Send the message with the correct client connection i.e.
1). Store all the connection of the clients in an array at the time of authentication.
2). When a message come find the correct client associated with the "to" field in the message stanza.
var receiverCleint = getClient(msgStanza.attrs.to)
3). return the correct client and send the message through that client.
receiverCleint.send(msgStanza)
I am also working on the same demo, my only concern is when more user adds then it will be a problem as it has to iterate on each message through all clients.
Any better approach appreciated.
Code:
var clients = new Array();
c2sRouter.on("connection", function(client){
client.on("register", function(opts, cb){
console.log("client server", "register");
})
client.on("authenticate", function(opts, cb){
console.log("client server", "authenticate");
if(opts.password === "tushar" || opts.password === "tushar1"){
clients.push(client);
cb(null, opts);
}else{
cb(false, opts);
}
})
client.on("online", function(){
console.log("client server", "online");
})
client.on("stanza", function(stanza){
console.log("client server", stanza.toString());
if(stanza.is("message")){
var receiverClient = getClient(stanza.attrs.to)
if(receiverClient === undefined){
client.send(stanza);
} else {
receiverClient.send(stanza);
}
}
})
client.on("disconnect", function(){
console.log("client server", "disconnect");
})
})
var getClient = function (to) {
var clientLength = clients.length;
var client
var clientId;
for(var i = 0; i < clientLength; i++){
client = clients[i];
clientId = client.jid.user + "@" + client.jid.domain
if(to.indexOf(clientId) == 0){
return client;
}
}
}