2013-06-26 49 views
0

我使用Jetty WebSockets作为服务器端,Autobahn Android作为客户端。Autobahn Android:如何断开服务器

服务器和客户端之间的简单连接工作正常。但是当我尝试处理丢失的连接时,我遇到了一些麻烦。

在Android上,我有什么是这样的:

private void connectToServer() { 

     try { 

      mConnection.connect(getString(R.string.server_addr), new WebSocketHandler(){ 

       // connection au serveur 
       @Override 
       public void onOpen(){ 
        Log.d(TAG, "Connected"); 
        Log.d(TAG, "First connexion, sending MAC @"); 
        Log.d(TAG, "My MAC Addr: "+ macAddr); 
        mConnection.sendTextMessage(macAddr); 

       } 
       // reception d'un message text 
       @Override 
       public void onTextMessage(String payload) { 

        //TODO 
       } 

       // fermeture de la connexion 
       @Override 
       public void onClose(int code, String reason) { 
        Log.d(TAG, "Connection lost. "+reason); 
        if(mConnection.isConnected()){ 
         Log.d(TAG, "Still connected, disconnect!"); 
         mConnection.disconnect(); 
        } 
        if(code<4000){ 
         int totalWaitTime = 0; 
         int waitTime = 0; 
         Log.d(TAG, "Should be disconnected"); 
         while(!mConnection.isConnected()){ 
          try { 
           waitTime= random.nextInt(MAX_TO_WAIT - MIN_TO_WAIT + 1) + MIN_TO_WAIT; 
           Log.d(TAG, "I'll wait "+waitTime+"ms"); 
           totalWaitTime +=waitTime; 
           Log.d(TAG, "Waiting for "+totalWaitTime+"ms"); 
           if(totalWaitTime <= HOUR_TO_MS){ 
            Thread.sleep(waitTime); 
            Log.d(TAG, "Trying to reconnect"); 
            connectToServer(); 
           }else{ 
            throw new InterruptedException("Attempt to connect to the server during 1 hours without success"); 
           } 
          } catch (InterruptedException e) { 
           e.printStackTrace(); 
          } 
         } 
        } 
       } 
      }); 
     } catch (WebSocketException e) { 

      Log.e(TAG, "Error on connect: "+e.toString()); 
      Log.d(TAG, "is connected: "+mConnection.isConnected()); 
      if(mConnection.isConnected()) 
       mConnection.disconnect(); 
      connectToServer(); 
     } 

    } 

我总是,总是有相同的错误:

06-26 10:36:07.823: E/wingo.stb.qos.AutoStartService(1842): Error on connect: de.tavendo.autobahn.WebSocketException: already connected 

但是就像你所看到的,我关闭的OnClose连接( ),并且当一个WebSocketException被捕获。这种方法真的有用吗?还是我做错了?

顺便说一句,mConnection是最终的。所以也许问题出现在这里?

private final WebSocketConnection mConnection = new WebSocketConnection(); 

在服务器上,当我有一个连接丢失我手动关闭会话:

@OnWebSocketClose 
    public void onClose(Session session, int closeCode, String closeReason){ 
      try { 
       System.out.println("connexion closed. Reason: "+closeReason); 
       pingPongTimer.cancel(); 
       if(session.isOpen()) 
        session.close(); 
       WebSocketsCentralisation.getInstance().leave(this); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
    } 

提前感谢真棒人!

+0

添加此行mConnection = null; –

+0

@ Poovizhirajan.N:我不能那样做mConnection是最终的! – brunettia

+0

好吧,等我等我和你电话联系 –

回答

0

所以,我有点混乱。我现在有什么作品:

private void connectToServer() { 
      try { 
       mConnection.connect(getString(R.string.server_addr), new WebSocketHandler(){ 

        // connection au serveur 
        @Override 
        public void onOpen(){ 
         Log.d(TAG, "Connected"); 
         Log.d(TAG, "First connexion, sending MAC @"); 
         Log.d(TAG, "My MAC Addr: "+ macAddr); 
         mConnection.sendTextMessage(macAddr); 

        } 
        // reception d'un message text 
        @Override 
        public void onTextMessage(String payload) { 

         //TODO 
        } 

        // fermeture de la connexion 
        @Override 
        public void onClose(int code, String reason) { 
         Log.d(TAG, "Connection lost. "+reason+" error code : "+code); 
         if(code<4000){ 
          reconnectToServer(); 
         } 
        } 
       }); 
      } catch (WebSocketException e) { 

       Log.e(TAG, "Error on connect: "+e.toString()); 
       Log.d(TAG, "is connected: "+mConnection.isConnected()); 
      } 

     } 

    private void reconnectToServer() { 
      try { 
       if(goConnect){ 
        goConnect = false; 
        Thread.sleep(1000); 
        Log.d(TAG, "DISCONNECT:"); 
        // mConnection.disconnect(); 
        Log.d(TAG, "ReconnectTimer Launched"); 
        new ReconnectTask().run(); 
       } 
      } catch (InterruptedException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      }  
      } 

    private class ReconnectTask extends TimerTask{ 
     @Override 
     public void run() { 
      try{ 
       if(totalWaitTime<HOUR_TO_MS){ 
        if(!mConnection.isConnected()){ 
         int waitTime= random.nextInt(MAX_TO_WAIT - MIN_TO_WAIT + 1) + MIN_TO_WAIT; 
         Log.d(TAG, "Next tentative to connect in "+waitTime+" ms"); 
         totalWaitTime +=waitTime; 
         reconnectTimer.schedule(new ReconnectTask(), waitTime); 
         connectToServer(); 
        }else{ 
         Log.d(TAG, "Connected to the server again"); 
         reinitializeReconnection(); 
        } 
       }else throw new InterruptedException("Attempt to connect to the server during 1 hours without success"); 
      }catch(InterruptedException e){ 
       Log.d(TAG, e.getMessage()); 
      } 

     } 
    } 

private void reinitializeReconnection(){ 
     reconnectTimer.purge(); 
     goConnect = true; 
     totalWaitTime = 0; 
    } 

基本上,我试图让所有的东西都出来WebSocketHandler。而我不明白的是,如果你试图通过“onClose”连接到服务器,并且服务器关闭了,它将再次继续“onClose”。所以我在做递归调用,而且非常混乱。

+0

你好,请问你如何instanciate reconnectTimer对象? – onizukaek