[...] public class ActivityMain extends Activity implements WebSocket.WebSocketConnectionObserver { private volatile boolean isConnected = false; private WebSocketConnection wsConnection; private WebSocketOptions wsOptions; @Override protected void onCreate (Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView (R.layout.activity_main); connectButton.setOnClickListener (new View.OnClickListener() { @Override public void onClick(View v) { wsConnect()); } }); cmdInput.setOnEditorActionListener (new EditText.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { wsSend(); return true; } return false; } }); } boolean wsConnect() { this.wsConnection = new WebSocketConnection(); this.wsOptions = new WebSocketOptions(); wsConnection.connect(wsURI, this, wsOptions); } void wsDisconnect() { wsConnection.disconnect(); } void wsSend() { /* send message to the server */ wsConnection.sendTextMessage(cmdInput.getText().toString()); appendText(cmdOutput, "[CLIENT] " + cmdInput.getText().toString() + "\n", Color.RED); } @Override public void onOpen() { this.isConnected = true; } @Override public void onClose (WebSocketCloseNotification code, String reason){ this.isConnected = false; } @Override public void onTextMessage (String payload) { JSONObject jsonObj = new JSONObject(payload); if ((jsonObj.has(TAG_JSON_TYPE)) && (jsonObj.has(TAG_JSON_MSG))) { if (jsonObj.getString(TAG_JSON_TYPE).equals("notification")) { /* show notification */ } else if (jsonObj.getString(TAG_JSON_TYPE).equals("standard")) { appendText(cmdOutput, "[SERVER]" + jsonObj.getString(TAG_JSON_MSG)+"\n",Color.RED); } else { Log.e (TAG_LOG, "Received invalid JSON from server"); } } } @Override public void onRawTextMessage (byte[] payload) { Log.wtf (TAG_LOG, "We didn't expect 'RawTextMessage'"); } @Override public void onBinaryMessage (byte[] payload) { Log.wtf (TAG_LOG, "We didn't expect 'BinaryMessage'"); } [...]