gpt4 book ai didi

java - java中如何通过Smack实现流管理?

转载 作者:太空宇宙 更新时间:2023-11-04 11:39:58 26 4
gpt4 key购买 nike

如何使用 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,如何在这里实现流管理?

最佳答案

创建连接对象后使用以下代码

 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'/>客户端和服务器之间进行交换。

关于java - java中如何通过Smack实现流管理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42922358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com