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

发布于 2024-09-09 03:14:38 字数 236 浏览 6 评论 0原文

client1 可以使用

connection.getChatManager().createChat(to, this).sendMessage(message);

如何发送其他类型的消息:如 INFO 消息? (客户端1到客户端2)?

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

非常感谢。

client1 can send txt message to client2 using

connection.getChatManager().createChat(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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花ソ最美的依靠 2024-09-16 03:14:38

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

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

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.

不爱素颜 2024-09-16 03:14:38

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

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

发送消息的过程:

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

UserInfo4XMPP定义为:

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

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;  
    }  
}
酷到爆炸 2024-09-16 03:14:38

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

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

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文