[英]how to disconnect client if it is already connected
我正在使用smack API和使用Java的2个客户端,我正在Windows平台上使用prosody XMPP服务器,这是我的代码(prosody与Open-fire服务器非常相似)
import java.util.Collection;
import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.Roster;
import org.jivesoftware.smack.RosterEntry;
import org.jivesoftware.smack.SASLAuthentication;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Message;
public class TextMessageReceive implements MessageListener {
XMPPConnection connection;
public void login(String userName, String password)throws XMPPException{
ConnectionConfiguration config = new ConnectionConfiguration("localhost",5222,"example.com");
config.setSASLAuthenticationEnabled(true);
config.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
SASLAuthentication.supportSASLMechanism("PLAIN", 0);
connection = new XMPPConnection(config);
//connection.connect();
// System.out.println("Connected to " + connection.getHost());
//connection.login(userName, password);
onTimerExpiry(userName, password);
System.out.println("Logged in as " + connection.getUser());
}
public void onTimerExpiry(String userName, String password) {
if (connection != null && connection.isConnected()) {
connection.disconnect();
System.out.println("disconnecting...................");
}
try{
if (connection != null) {
connection.connect();
System.out.println("Logged in as " + connection.getHost());
connection.login(userName, password);
System.out.println("Logged in as " + connection.getUser());
}
}catch(XMPPException e){
e.printStackTrace();
}
}
public void sendMessage(String message, String to) throws XMPPException{
Chat chat = connection.getChatManager().createChat(to, this);
chat.sendMessage(message);
System.out.println("message= "+message+ " received from " +to );
}
public void displayBuddyList(){
Roster roster = connection.getRoster();
Collection<RosterEntry> entries = roster.getEntries();
System.out.println("\n\n" + entries.size() + " buddy(ies):");
for(RosterEntry r:entries){
System.out.println(r.getUser());
}
}
public void disconnect(){
connection.disconnect();
}
public void processMessage(Chat chat, Message message){
System.out.println("Inside the process message");
String string =message.getBody();
String from=message.getFrom();
// byte[] byteArray = Base64.decodeBase64(string.getBytes());
// if(message.getType() == Message.Type.chat)
// {
//// // Print the decoded string
// String decodedString = new String(byteArray);
// System.out.println( "Decoded message = " + decodedString);
System.out.println("message= "+string+ " received from " +from );
}
public static void main(String args[]) throws XMPPException {
TextMessageReceive c = new TextMessageReceive();
// turn on the enhanced debugger
XMPPConnection.DEBUG_ENABLED = true;
// Enter your login information here
c.login("yuvi", "yuvi");
/*
* Displays the users list
*/
//c.displayBuddyList();
try{
Thread.sleep(30000);
String string ="We are sending message to mhealth";
// byte[] byteArray = Base64.encodeBase64(string.getBytes());
// String encodedString = new String(byteArray);
c.sendMessage(string, "mhealth@example.com");
// Print the encoded string
System.out.println( "Message = " + string);
// c.sendMessage("Message coming from yuvi", "yuvi@inpudw014438a");
}catch (Exception e) {
e.printStackTrace();
}
}
}
实际上,如果我是第一次登录,那么我将使用prosody配置该特定的代码,然后代码没有问题,但是每当我第二次尝试连接时,除非从telnet管理控制台关闭客户端,否则我都会收到异常。在这里我做了onTimerExpiry( )断开客户端连接的方法(如果客户端已经连接但客户端正在断开连接并给我以下异常)。 伙计们,请告诉我对还是错,请建议我...
我得到的异常是:
stream:error (text) at
org.jivesoftware.smack.PacketReader.parsePackets(PacketReader.java:260) at
org.jivesoftware.smack.PacketReader.access$000(PacketReader.java:43) at
org.jivesoftware.smack.PacketReader$1.run(PacketReader.java:70)
我得到了答案。我必须从main()调用Disconnect()方法
public static void main(String args[]){
TextMessageReceive c = new TextMessageReceive();
// turn on the enhanced debugger
XMPPConnection.DEBUG_ENABLED = true;
// Enter your login information here
c.login("yuvi", "yuvi");
c.disconnect();
/*
* Displays the users list
*/
}
这是可行的&在o / p中没有任何异常
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.