首页 > 代码库 > Java用Smack实现简单地通过OpenFire与其他人进行聊天

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与其他人进行聊天