如何在java中通过Smack实现流管理?
如何使用java使用smack 4.1中的XEP 198(流管理),请分享一些参考资料 我正在使用java代码将消息从smack发送到spark如何在java中通过Smack实现流管理?,java,xmpp,smack,Java,Xmpp,Smack,如何使用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 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();
}
私有静态字符串username=“test”;
私有静态字符串password=“test123”;
公共静态类MessageParrot实现PacketListener{
专用XMPPConnection XMPPConnection;
公共消息鹦鹉(XMPPConnection conn){
xmppConnection=conn;
}
公共无效处理数据包(数据包){
消息消息=(消息)数据包;
if(message.getBody()!=null){
String fromName=StringUtils.parseBareAddress(message.getFrom());
System.out.println(“来自“+fromName+”\n“+Message.getBody()+”\n”)的消息;
消息回复=新消息();
回复:setTo(fromName);
reply.setBody(“我是一个Java机器人,你说:”+message.getBody());
xmppConnection.sendPacket(应答);
}
}
}
公共静态void main(字符串[]args){
System.out.println(“启动IM客户端”);
//gtalk要求此消息或您的消息作为错误返回
ConnectionConfiguration ConnconConfig=新的ConnectionConfiguration(“domain.com”,5222);
XMPPConnection连接=新的XMPPConnection(connconconfig);
试一试{
connection.connect();
System.out.println(“连接到”+connection.getHost());
}捕获(XMPPException-ex){
//例如printStackTrace();
System.out.println(“未能连接到”+connection.getHost());
系统出口(1);
}
试一试{
连接。登录(用户名、密码);
System.out.println(“以“+connection.getUser()登录”);
状态=新状态(Presence.Type.available);
连接。发送数据包(存在);
}捕获(XMPPException-ex){
//例如printStackTrace();
//XMPPConnection仅在登录成功时才记住用户名
//因此,除非正确登录,否则无法使用connection.getUser()
System.out.println(“未能以“+用户名”登录);
系统出口(1);
}
PacketFilter=newmessagetypefilter(Message.Type.chat);
addPacketListener(新建MessageParrot(连接),过滤器);
/*如果(args.length>0){*/
//谷歌回复默认的消息类型,你必须使用聊天
Message msg=新消息(“test2@domain.com,Message.Type.chat);
msg.setBody(“hi”);
//msg.addExtension(新的DeliveryReceipt(msg.getPacketID());
/*xhtml-lextension xhtml-lextension=新的xhtml-lextension();
xhtmlExtension.addBody(
“大人,快去读这些文章。”);
msg.addExtension(xhtmlExtension)*/
/*MessageEventManager.addNotificationsRequests(消息,真,真,真,真);
DeliveryReceiptManager.addDeliveryReceiptRequest(msg)*/
连接发送包(msg);
System.out.println(“按enter键断开\n”);
试一试{
System.in.read();
}捕获(IOEX异常){
//例如printStackTrace();
}
连接断开();
}
通过这个,我可以与两个客户端聊天,一个来自smack,另一个来自spark,如何在这里实现流管理?创建连接对象后使用下面的代码
connection.setUseStreamManagement(true);
connection.setUseStreamManagementResumptionDefault(true);
完成后,请查看
的日志,这意味着客户端希望使用流管理。服务器将对此做出响应(如果支持)
,这意味着流现在已启用
一旦完成,如果您经常检查日志,您会发现客户机和服务器之间发生了
和
交换。在创建连接对象后使用下面的代码
connection.setUseStreamManagement(true);
connection.setUseStreamManagementResumptionDefault(true);
完成后,请查看
的日志,这意味着客户端希望使用流管理。服务器将对此做出响应(如果支持)
,这意味着流现在已启用
一旦完成,如果您经常检查日志,您会发现客户机和服务器之间发生了
和
交换