I've been splitting hairs, trying to accomplish what I though would be simple and straight forward (writing a handler which triggers an action once a user joins a chat room).
So here's the line for joining the chat room
Code:
My_namespace.connection.send(
$pres({to: My_namespace.room_jid + "/" + My_namespace.nick, id: "join_room"}).c("jid").t(My_namespace.jid).up().c("nick").t(My_namespace.nick)
);
And here is the line that sets the handler before initial presence is sent
Code:
My_namespace.connection.addHandler(My_namespace.room_joined, null, "presence", null, "join_room");
And here's the handler implementation
Code:
My_namespace.room_joined = function(presence){
var nick = $(presence).children("nick").text();
var jid = $(presence).children("jid").text();
console.log("Successfully joined room. Nick is: "+nick+" and jid is: "+jid );
return true;
}
This doesn't work. I suspected that you cannot use ids with such stanzas. So i stripped the id attribute and tried this instead, similar to what's in the book
Code:
My_namespace.room_joined = function(presence){
var from = $(presence).attr("from");
if(from === My_namespace.room_jid + "/" + My_namespace.nick){//If presence is coming from me
var nick = $(presence).children("nick").text();
var jid = $(presence).children("jid").text();
console.log("Successfully joined room. Nick is: "+nick+" and jid is: "+jid );
}
return true;
}
This kind of works, but only when I test it on the iPhone. For some reason I do not understand, it either won't run on a pc or mac, or it just takes for ever to trigger the handler. Who knows how to best go about this?