【发布时间】:2017-03-21 08:58:30
【问题描述】:
如何使用 smack 4.1 中的 XEP 198(流管理)使用 java ,请分享一些参考。
我正在使用 java 代码将消息从 smack 发送到 spark
private static String username = "test";
private static String password = "test123";
public static class MessageParrot implements PacketListener {
private XMPPConnection xmppConnection;
public MessageParrot(XMPPConnection conn) {
xmppConnection = conn;
}
public void processPacket(Packet packet) {
Message message = (Message)packet;
if(message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
System.out.println("Message from " + fromName + "\n" + message.getBody() + "\n");
Message reply = new Message();
reply.setTo(fromName);
reply.setBody("I am a Java bot. You said: " + message.getBody());
xmppConnection.sendPacket(reply);
}
}
}
public static void main(String[] args ) {
System.out.println("Starting IM client");
// gtalk requires this or your messages bounce back as errors
ConnectionConfiguration connConfig = new ConnectionConfiguration("domain.com", 5222);
XMPPConnection connection = new XMPPConnection(connConfig);
try {
connection.connect();
System.out.println("Connected to " + connection.getHost());
} catch (XMPPException ex) {
//ex.printStackTrace();
System.out.println("Failed to connect to " + connection.getHost());
System.exit(1);
}
try {
connection.login(username, password);
System.out.println("Logged in as " + connection.getUser());
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
} catch (XMPPException ex) {
//ex.printStackTrace();
// XMPPConnection only remember the username if login is succesful
// so we can''t use connection.getUser() unless we log in correctly
System.out.println("Failed to log in as " + username);
System.exit(1);
}
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new MessageParrot(connection), filter);
/* if(args.length > 0) {*/
// google bounces back the default message types, you must use chat
Message msg = new Message("test2@domain.com", Message.Type.chat);
msg.setBody("hi");
// msg.addExtension(new DeliveryReceipt(msg.getPacketID()));
/* XHTMLExtension xhtmlExtension = new XHTMLExtension();
xhtmlExtension.addBody(
"<body>My lord, dispatch; read o'er these articles.</body><request xmlns='urn:xmpp:receipts'/>");
msg.addExtension(xhtmlExtension);*/
/* MessageEventManager.addNotificationsRequests(msg, true, true, true, true);
DeliveryReceiptManager.addDeliveryReceiptRequest(msg);*/
connection.sendPacket(msg);
System.out.println("Press enter to disconnect\n");
try {
System.in.read();
} catch (IOException ex) {
//ex.printStackTrace();
}
connection.disconnect();
}
通过这个我可以和两个客户端聊天,一个来自 smack,另一个来自 spark,如何在这里实现流管理?
【问题讨论】: