如何使用java中的smack 4.1使用XEP 198(流管理),请分享一些参考。
我正在使用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,如何实现流管理?
答案 0 :(得分:0)
创建连接对象后使用以下代码
connection.setUseStreamManagement(true);
connection.setUseStreamManagementResumptionDefault(true);
在此之后检查<enable xmlns='urn:xmpp:sm:3' resume='true'/>
的日志,这意味着客户端想要使用流管理。服务器将响应此(如果支持)<enabled xmlns='urn:xmpp:sm:3' id='.......' resume='true' max='1'/>
,这意味着现在启用了流
如果您不断检查日志,一旦完成,您会发现<r xmlns='urn:xmpp:sm:3'/>
和<a xmlns='urn:xmpp:sm:3' h='1'/>
在客户端和服务器之间进行交换。