除聊天文本之外的Smack消息传递客户端

时间:2010-07-11 03:06:43

标签: java xmpp smack

client1可以使用

向客户端2发送txt消息
connection.getChatManager().createChat(to, this).sendMessage(message);

如何发送其他类型的消息:如INFO消息? (client1到client2)?

因为我想要,不应该在聊天窗口中显示INFO消息....是否可能?使用smack,smackx库。

非常感谢。

3 个答案:

答案 0 :(得分:2)

您要做的是在短信中添加扩展名。这是一个粗略的指南。

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)

属性在一个单独的命名空间中携带(使用toXML()来查看它),并且不会出现在聊天窗口中。在“另一边”,应用程序使用getProperty()来检索值。

如果您不想要键/值对,但需要结构化数据,请在邮件正文中使用addExtension()。这有点复杂,因为您基本上必须扩展PacketExtension,提供自己的命名空间。您甚至可能需要编写PacketExtensionProvider

答案 1 :(得分:0)

这里是消息扩展的示例。 XMPP协议的基本XML结构为:

it is raining a cat and a dog
contains profanity

it is raining a cats and a dogs
a clean sentence

发送消息的过程:

<message  
    from='sender_jid'  
    to='receiver_jid'  
    type='message_type'>  
  <body> content </body>  
</message> 

UserInfo4XMPP定义为:

//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); 

答案 2 :(得分:0)

一个更复杂的消息扩展示例:

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);

扩展类VideoChatRIP必须实现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());

处理:

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;
    }
}