client1 can send txt message to client2 using
connection.getChatManager().crea开发者_JS百科teChat(to, this).sendMessage(message);
How to send other kind of message: like INFO message ? (client1 to client2) ?
Because I want, INFO message should not be displayed in the chat window....is it possible ? using smack, smackx library.
Many Thanks.
What you want do do here is to add an extension to your text message. Here is a rough guide.
Chat chat = connection.getChatManager().createChat(to, this);
Message message = new Message(to, Message.Type.chat);
message.setBody("hello");
message.setProperty(prop0, val0);
message.setProperty(prop1, val1);
chat.sendMessage(message)
The properties are carried in a separate namespace (use toXML()
to look at it) and will not appear in the chat window. On the 'other side', the applications uses getProperty()
to retrieve the value.
If you do not want key/value pairs, but structured data, use addExtension()
in the message body. This is a little more complicated because you basically have to extend PacketExtension
, provide your own namespace. You might even have to write a PacketExtensionProvider
.
Here is the example for message extension. The basic XML structure of the XMPP protocol is:
<message
from='sender_jid'
to='receiver_jid'
type='message_type'>
<body> content </body>
</message>
The process of sending messages:
//build chat
Chat chat = chatManager.createChat("jid");
//build extension
UserInfo4XMPP userInfo4XMPP = new UserInfo4XMPP();
userInfo4XMPP.setNameText("userx");
userInfo4XMPP.setUrlText("http://www.liaoku.org/");
//build message
Message message = new Message();
message.setBody("hello");
message.addExtension(userInfo4XMPP);// add the extension
//send
chat.sendMessage(message);
The UserInfo4XMPP is defined as:
import org.jivesoftware.smack.packet.ExtensionElement;
public class UserInfo4XMPP implements ExtensionElement{
private String elementName = "userinfo";
private String nameElement = "name";
private String urlElement = "url";
private String nameText = "";
private String urlText = "";
@Override
public String getElementName() {
return elementName;
}
@Override
public CharSequence toXML() {
StringBuilder sb = new StringBuilder();
sb.append("<");
sb.append(elementName);
sb.append(">");
sb.append("<");
sb.append(nameElement);
sb.append(">");
sb.append(nameText);
sb.append("</");
sb.append(nameElement);
sb.append(">");
sb.append("<");
sb.append(urlElement);
sb.append(">");
sb.append(urlText);
sb.append("</");
sb.append(urlElement);
sb.append(">");
sb.append("</");
sb.append(elementName);
sb.append(">");
return sb.toString();
}
@Override
public String getNamespace() {
return "";
}
public String getNameText() {
return nameText;
}
public void setNameText(String nameText) {
this.nameText = nameText;
}
public String getUrlText() {
return urlText;
}
public void setUrlText(String urlText) {
this.urlText = urlText;
}
}
A more complicated example of message extention:
Message videoMsg = new Message();
VideoChatRTP videoXml = new VideoChatRTP();
videoXml.setVideoType(VideoMediaType.REQUEST);
videoXml.setAddress(address);
videoMsg.setTo(receive);
videoMsg.addExtension(videoXml);
XMPPConnection conn = BaseService.getInstance().getConnection();
conn.sendPacket(videoMsg);
The extension class VideoChatRIP must implement PacketExtension:
public class VideoChatRTP implements PacketExtension {
private VideoMediaType videoType;
private String address;
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public VideoMediaType getVideoType() {
return videoType;
}
public void setVideoType(VideoMediaType videoType) {
this.videoType = videoType;
}
public static final String NAME = "jingle";
public static final String NAME_SPACE = "com:roger:video";
public VideoChatRTP() {
super();
}
@Override
public String getElementName() {
return NAME;
}
@Override
public String getNamespace() {
return NAME_SPACE;
}
@Override
public String toXML() {
StringBuffer sb = new StringBuffer();
sb.append("<jingle").append(" xmlns=\"").append(NAME_SPACE).append(
"\">");
sb.append("<type>").append(videoType).append("</type>");
sb.append("<ipAddress>").append(address).append("</ipAddress>");
sb.append("</jingle>");
return sb.toString();
}
}
The Listener:
ProviderManager.getInstance().addExtensionProvider(VideoChatRTP.NAME, VideoChatRTP.NAME_SPACE, new VideoChatReceive());
Processing:
public class VideoChatReceive implements PacketExtensionProvider {
@Override
public PacketExtension parseExtension(XmlPullParser parser)
throws Exception {
boolean done = false;
String requestType = "asdasd";
String ipAddress = "asdasd";
while (!done) {
int eventType = parser.next();
String name = parser.getName();
if (eventType == XmlPullParser.START_TAG) {
if (name.equals("type")) {
requestType = parser.nextText();
}
if (name.equals("ipAddress")) {
ipAddress = parser.nextText();
}
}
if (eventType == XmlPullParser.END_TAG) {
if (name.equals("jingle")) {
done = true;
}
}
}
VideoChatRTP videoRtp = new VideoChatRTP();
videoRtp.setVideoType(VideoMediaType.valueOf(requestType));
videoRtp.setAddress(ipAddress);
return videoRtp;
}
}
精彩评论