How to send Private Messages

Hello, It’'s me again.

I just wonder where can I read about XIFF (besides the classes documentation), because I seem to be failing to send private messages within a room.

I posted this question 10 days ago, but with no luck. Is there anybody out there?

I’'m testing the XIFFExample.fla file that comes with the XIFF package.

Everything is fine. But now I want to send private messages between users in a room and I tried this:

chatRoom.sendPrivateMessage ( “SomeNickName”, “Some private message body text”, “”);

I can see the traced XML with the message arriving only to the intended client. But the Room object is not dispatching the “privateMessage” event.

Am I sending the message in a wrong way?

Thanks,

JulianG

looking at the code it should work…

anyway, if you see the message arriving at the receiver side, try debugging the

lines 630 to 632 in Room.as

else if( isThisUser(msg.to) && msg.type == Message.CHAT_TYPE ) {

dispatchEvent( {target:this, type:“privateMessage”, data:msg} );

}

i think the isThisUser(msg.to) could be wrong, the server rewrites the msg.to from

roomId@server/nickname to realUser@server/resource and the isThisUser(msg.to) fails then.

It could be that the isThisUser(msg.to) should be (msg.to == myConnection.myJID)

anyway hopes this helps

(sorry for bumping) Nope, that didn’‘t help. Still can’'t catch the “privateMessage” event. Any other ideas?

have you tried by adding the event listener to the room?

do you have some chatRoom.addEventListener(“privateMessage”, eventHandler);[/i] where chatRoom is your room instance.

Maybe this is a dumb question, but I thought the point of IM was to send messages between two individual clients. So why does the example focus on chat rooms instead of user-to-user messaging? And how does one go about doing that – without a “room” object – just based on the other person’'s username?

OK, I figured out how to send a single message by deconstructing some of the work from XIFFIAN:

import org.jivesoftware.xiff.data.Message;

import org.jivesoftware.xiff.core.XMPPConnection;

var connection = new XMPPConnection();

connection.username = “*********”;

connection.password = “*********”;

connection.server = “***********”;

var eventHandler = new Object();

eventHandler.handleEvent = function( eventObj )

{

switch (eventObj.type) {

case “login”:

var msg = new Message(“user@server/client”, null, “Hello, world.”, “foo”, Message.CHAT_TYPE);

_root.connection.send(msg);

break;

}

connection.addEventListener(“login”, eventHandler);

Stage.addListener( eventHandler );

connection.connect( “flash” );[/i]

simple example to wait until you’'re logged in and then send a message to someone else. Of course, using event dispatching you can get much fancier than that.

It’‘s strange that I had to use a full application as reference, since XIFF calls itself an API. But every serious API I’'ve ever worked with prides itself on thorough class documentation, with all methods and parameters of each object well described and often simple working examples provided for a variety of cases. Did I just miss that part of the site? Sure would be nice to have something as good as the AS Dictionary for XIFF…

OK, I finally figured out how to trap a private message. For some reason the XIFF library only treats privateMessage and chat XML types as incomingData. So I had to hack the incomingData trap, re-parse the XML, and re-throw the event in order to finally get it to work. But it does work. I just feel like I’'m solving a problem that should have been solved in the API, and that this is entirely the wrong layer to be dealing with this stuff.

//…

eventHandler.handleEvent = function( eventObj )

{

switch( eventObj.type )

{

case “incomingData”:

var myXML_xml = new XML();

myXML_xml.parseXML(eventObj.data);

var newEvent = myXML_xml.firstChild.attributes;

if (newEvent.type == “chat”) {

newEvent.data = eventObj.data;

newEvent.data.body = myXML_xml.firstChild.firstChild.firstChild.nodeValue;

newEvent.data.from = newEvent.from;

newEvent.data.to = newEvent.to;

this.handleEvent( newEvent );

}

break;

//…

case “chat”:

var msg = eventObj.data;

var nick = msg.from.split( “@” )[0];

addToPrivateChatOutput( nick, msg.body );

break;

}

//…

}

//…

connection.addEventListener( “incomingData”, eventHandler );

connection.addEventListener( “login”, eventHandler );

//…

/i

Can someone tell me a better way? b0ris? Did you have to do this to get XIFFIAN to accept PMs?

You probably figured this out by now…

just listen for the “message” event from your XMPPConnection…

obj.data will be of type Message

use it from there…

I was having problems with private messages as well and I think that Bram is right in saying that there is a problem with the code in the Room.as class, around line 630. The main problem is that the private message gets caught in the “if( isThisRoom( msg.from ) )” branch of the conditional since the message is coded as coming from the room (no differently than a group message).

I was able to fix it by inserting the else if branch that handles the Message.CHAT_TYPE type messages inside the conditional right above it. The following code seems to work for me though I haven’'t tested it extensively:

if( isThisRoom( msg.from ) ) {

if ( msg.type == Message.GROUPCHAT_TYPE ) {

// Check for a subject change

if( msg.subject != null ) {

mySubject = msg.subject;

dispatchEvent( {target:this, type:“subjectChange”, subject:msg.subject} );

}

else {

dispatchEvent( {target:this, type:“groupMessage”, data:msg} );

}

} else if ( msg.type == Message.NORMAL_TYPE ) {

var form = msg.getAllExtensionsByNS(FormExtension.NS)[0];

if (form) {

dispatchEvent({type: “configureForm”, target: this, data:form});

}

}

// It could be a private message via the conference

else if(msg.type == Message.CHAT_TYPE ) {

dispatchEvent( {target:this, type:“privateMessage”, data:msg} );

}

}