首页 > 代码库 > Smack Message 扩展属性

Smack Message 扩展属性

由于XMPP不支持群聊很多本地化的服务,

针对Openfire开发插件解决问题:

Message 扩展:

Message videoMsg = new Message();
VideoChatRTP videoXml = new VideoChatRTP();
//扩展类型
videoXml.setVideoType(VideoMediaType.REQUEST);
//请求类型
videoXml.setAddress(address);
//地址
videoMsg.setTo(receive);
videoMsg.addExtension(videoXml);
//message扩展

XMPPConnection conn = BaseService.getInstance().getConnection();
conn.sendPacket(videoMsg);

扩展类必须继承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();
}
}

扩展类型监听方法:

ProviderManager.getInstance().addExtensionProvider(VideoChatRTP.NAME, VideoChatRTP.NAME_SPACE, new VideoChatReceive());

扩展类型处理类(实现PacketExtensionProvider接口):

public class VideoChatReceive implements PacketExtensionProvider {

/**
* 处理视频扩展类型message
* @author bin.xiao
* @since 2013-04-12
* */
@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();
//XML Tab标签
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;
}
}


Smack Message 扩展属性