如果客户端已连接,如何断开连接

时间:2014-03-31 10:59:25

标签: java

我正在使用smack API&使用java制作2个客户端,我在Windows平台上使用韵律XMPP服务器这是我的代码(韵律与开放式服务器非常相似)

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();
        }
    }
}

实际上我正在使用针对特定代码的韵律配置,如果我第一次登录然后没有代码问题,但每当我尝试连接第二次我得到异常,除非我从telnet管理控制台关闭客户端。我使用onTimerExpiry()方法断开客户端,如果客户端已连接但我的客户端正在断开连接&amp;给了我以下例外。请告诉我这是对还是错,请建议我......

我得到的例外是:

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)

1 个答案:

答案 0 :(得分:0)

我得到了答案。我必须从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
             */
}

这是有效的&amp;在o / p中没有任何例外