Last active
July 22, 2016 11:05
-
-
Save arkangelx/4c644d253d534d3f3bbd9b4c1eccbcc0 to your computer and use it in GitHub Desktop.
Minimal example of android activity to debug websocket response error
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package gaming.eatongate.com.kwiffapp; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.widget.Toast; | |
import com.github.nkzawa.socketio.client.Ack; | |
import org.json.JSONException; | |
import org.json.JSONObject; | |
import io.socket.client.Socket; | |
import io.socket.emitter.Emitter; | |
public class WebsocketActivity extends AppCompatActivity { | |
public static final String TAG = WebsocketActivity.class.getSimpleName() + "RESPONSE"; | |
private JSONObject loginPayload; | |
public static final String SESSION_STRING_COMMAND = "command"; | |
private Socket mSocket; | |
@Override | |
protected void onResume() { | |
super.onResume(); | |
mSocket = KwiffApplication.getKwiffApplicationInstance().getSocket(); | |
mSocket.on(Socket.EVENT_CONNECT, new Emitter.Listener() { | |
@Override | |
public void call(Object... args) { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
Toast.makeText(WebsocketActivity.this, | |
"Connected", Toast.LENGTH_LONG).show(); | |
attemptLogin(); | |
} | |
}); | |
} | |
}); | |
mSocket.on(Socket.EVENT_DISCONNECT, new Emitter.Listener() { | |
@Override | |
public void call(Object... args) { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
Toast.makeText(WebsocketActivity.this, | |
"DisConnected", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
} | |
}); | |
mSocket.on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() { | |
@Override | |
public void call(Object... args) { | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
Toast.makeText(WebsocketActivity.this, | |
"Error of some sort", Toast.LENGTH_LONG).show(); | |
} | |
}); | |
} | |
}); | |
mSocket.connect(); | |
} | |
public void attemptLogin() { | |
try { | |
loginPayload = new JSONObject(); | |
loginPayload.put("name", "tyrion lannister"); | |
mSocket.emit(SESSION_STRING_COMMAND, loginPayload, new IOAcknowledge()); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} | |
@Override | |
public void onDestroy() { | |
super.onDestroy(); | |
mSocket.disconnect(); | |
} | |
private class IOAcknowledge implements Ack { | |
@Override | |
public void call(Object... args) { | |
JSONObject data = (JSONObject) args[0]; | |
Log.i(TAG, " Login confirmed: " + data.toString()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment