0

I am working on Web socket communication with Autobahn library. The problem I have is after connecting server, then message should be sent without connection again. But the message is sent with different connection that it connects to server every single time to send a message.

public class WebSocket_Connector extends Activity{

private static final String TAG = "ECHOCLIENT";
private static final String TAG1 = "My app";
public final WebSocketConnection mConnection = new WebSocketConnection();
private String tmpString = "";

public void connect(final String wsuri) {
Log.d(TAG, "Connecting to: " + wsuri); 
    try {
        mConnection.connect(wsuri, new WebSocketHandler() {
        @Override
           public void onOpen() {
           Log.d(TAG, "Status: Connected to " + wsuri ); 
           Log.d(TAG, "Connection successful!\n");
           mConnection.sendTextMessage(tmpString); 
           tmpString = "";
           }

           @Override
           public void onTextMessage(String payload) {
           Log.d(TAG, "Got echo: " + payload);
           }

           @Override
           public void onClose(int code, String reason) {
           Log.d(TAG, "Connection closed.");
           }
           });
       } catch (WebSocketException e) {    
           Log.d(TAG, e.toString());
           }
   }

public void sendMessage(String message) {
    if (mConnection.isConnected()) {
        Log.d(TAG1, "Messeage is sent : " + message);
        mConnection.sendTextMessage(message); 
        }
    else {
        tmpString = message;
        connect("ws://192.168.3.100:7681");
        }
    }   
}

This is the code I have, and...When you see "sendMessage" method, it always goes to 'else' not, if loop. Any suggestion 'experts' please..?

B770
  • 1,272
  • 3
  • 17
  • 34
user2500696
  • 65
  • 1
  • 7
  • I'm using Autobahn as well. I do not check ws.isConnected() each time, but check for thrown exceptions and error messages, and it's working fine. – damian Jun 21 '13 at 13:01
  • @damian then how can I check for thrown exceptions with if state? – user2500696 Jun 21 '13 at 13:03

1 Answers1

0

i don't know the package name you are dealing with for websocket. So first it has to be provided to get reliable answer to your question. But let say if it is something similar to : https://code.google.com/p/weberknecht/source/browse/trunk/src/main/de/roderick/weberknecht/WebSocketConnection.java?r=2 note: i have not seen there isConnected() method but assume that it is added somewhere else.

you can see from source that onOpen() (line 88) is called before connected = true; on line (91). if this member var will be used as result of isConnected() then your code always will follow "else" part of the condition.

i would advice to dig into websocket api and its usage pattern further.

Asaf Sh.
  • 49
  • 2
  • package name is http://autobahn.ws/static/reference/android/_web_socket_connection_8java_source.html – user2500696 Jun 21 '13 at 13:13
  • check how the asynch task must be executed: http://developer.android.com/reference/android/os/AsyncTask.html specifically: Once created, a task is executed very simply: new DownloadFilesTask().execute(url1, url2, url3); – Asaf Sh. Jun 21 '13 at 17:43