Java用Smack实现简单地通过OpenFire与其他人进行聊天

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java用Smack实现简单地通过OpenFire与其他人进行聊天相关的知识,希望对你有一定的参考价值。

import java.util.Objects;

import org.jivesoftware.smack.AbstractXMPPConnection;
import org.jivesoftware.smack.ConnectionConfiguration.SecurityMode;
import org.jivesoftware.smack.chat.Chat;
import org.jivesoftware.smack.chat.ChatManager;
import org.jivesoftware.smack.chat.ChatManagerListener;
import org.jivesoftware.smack.chat.ChatMessageListener;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.tcp.XMPPTCPConnection;
import org.jivesoftware.smack.tcp.XMPPTCPConnectionConfiguration;

public class SmackClient {

    public static void main(String[] args) {
        try {
            String serviceName = "XXXXXX";
            String host = "192.168.0.1";
            int port = 5222;

            String myUsername = "user1";
            String myPassword = "123456";

            String friendUsername = "user2";

            XMPPTCPConnectionConfiguration.Builder builder = XMPPTCPConnectionConfiguration.builder();
            builder.setUsernameAndPassword(myUsername, myPassword);
            builder.setServiceName(serviceName);
            builder.setHost(host);
            builder.setPort(port);
            builder.setSecurityMode(SecurityMode.disabled);
            builder.setDebuggerEnabled(false);

            XMPPTCPConnectionConfiguration config = builder.build();

            AbstractXMPPConnection c = new XMPPTCPConnection(config);

            c.connect();
            c.login();
            System.out.println("Authenticated = " + c.isAuthenticated());

            ChatManager chatmanager = ChatManager.getInstanceFor(c);
            Chat newChat = chatmanager.createChat(friendUsername + "@" + serviceName);

            final ChatMessageListener messageListener = new ChatMessageListener() {
                @Override
                public void processMessage(Chat arg0, Message message) {
                    String messageBody = message.getBody();
                    if (Objects.nonNull(messageBody)) {
                        try {
                            arg0.sendMessage("你刚说的是:" + messageBody);
                        } catch (Exception e) {
                        }
                    }
                }
            };

            ChatManagerListener chatManagerListener = new ChatManagerListener() {
                @Override
                public void chatCreated(Chat chat, boolean arg1) {
                    chat.addMessageListener(messageListener);
                }
            };
            chatmanager.addChatListener(chatManagerListener);

            try {
                newChat.sendMessage("我来咯");
            } catch (Exception e) {
                System.out.println("Error Delivering block");
            }

            while (true)
                ;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

附件为所需要的所有类库Jar包。

本文出自 “zl1030的记录” 博客,请务必保留此出处http://zl1030.blog.51cto.com/274507/1846938

以上是关于Java用Smack实现简单地通过OpenFire与其他人进行聊天的主要内容,如果未能解决你的问题,请参考以下文章

Android基于XMPP Smack及Openfire学习笔记

openfire+smack怎么实现类似于qq群的功能?

Openfire即时通讯服务搭建与Smack访问

(转)openfire插件开发通过http方式向openfire客户端发信息

基于openfire+smack即时通讯instant message开发

无法通过使用 smack、openfire 获得名册