Created
April 8, 2016 13:20
-
-
Save AndreiD/8145461d69fbbba9e79d5c4e14fb21ff to your computer and use it in GitHub Desktop.
test android sockets
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 com.androidadvance.testsockets; | |
import android.os.Bundle; | |
import android.support.design.widget.FloatingActionButton; | |
import android.support.v7.app.AppCompatActivity; | |
import android.support.v7.widget.Toolbar; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.TextView; | |
import com.neovisionaries.ws.client.OpeningHandshakeException; | |
import com.neovisionaries.ws.client.StatusLine; | |
import com.neovisionaries.ws.client.WebSocket; | |
import com.neovisionaries.ws.client.WebSocketAdapter; | |
import com.neovisionaries.ws.client.WebSocketException; | |
import com.neovisionaries.ws.client.WebSocketFactory; | |
import com.neovisionaries.ws.client.WebSocketFrame; | |
import java.util.List; | |
import java.util.Map; | |
public class MainActivity extends AppCompatActivity { | |
private TextView textView_received; | |
private WebSocket ws; | |
@Override protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
textView_received = (TextView) findViewById(R.id.textView_received); | |
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); | |
fab.setOnClickListener(new View.OnClickListener() { | |
@Override public void onClick(View view) { | |
ws.sendText("Hello. " + String.valueOf(System.currentTimeMillis())); | |
ws.sendPing("Are you there?"); | |
} | |
}); | |
new SocketAsyncTask().execute(); | |
} | |
//-------- DEBUGS THE SOCKET CONNECTION --------- | |
private void show_debug_info(OpeningHandshakeException e) { | |
// Status line. | |
StatusLine sl = e.getStatusLine(); | |
System.out.println("=== Status Line ==="); | |
System.out.format("HTTP Version = %s\n", sl.getHttpVersion()); | |
System.out.format("Status Code = %d\n", sl.getStatusCode()); | |
System.out.format("Reason Phrase = %s\n", sl.getReasonPhrase()); | |
// HTTP headers. | |
Map<String, List<String>> headers = e.getHeaders(); | |
System.out.println("=== HTTP Headers ==="); | |
for (Map.Entry<String, List<String>> entry : headers.entrySet()) { | |
// Header name. | |
String name = entry.getKey(); | |
// Values of the header. | |
List<String> values = entry.getValue(); | |
if (values == null || values.size() == 0) { | |
// Print the name only. | |
System.out.println(name); | |
continue; | |
} | |
for (String value : values) { | |
// Print the name and the value. | |
System.out.format("%s: %s\n", name, value); | |
} | |
} | |
} | |
private class SocketAsyncTask extends android.os.AsyncTask { | |
@Override protected Object doInBackground(Object[] objects) { | |
WebSocketFactory factory = new WebSocketFactory().setConnectionTimeout(5000); | |
try { | |
ws = factory.createSocket("ws://echo.websocket.org"); | |
try { | |
ws.connect(); | |
} catch (OpeningHandshakeException e) { | |
show_debug_info(e); | |
} | |
} catch (Exception e) { | |
Log.e("error", e.getMessage()); | |
} | |
return null; | |
} | |
@Override protected void onPostExecute(Object o) { | |
super.onPostExecute(o); | |
setup_listeners(); | |
} | |
} | |
private void setup_listeners() { | |
ws.addListener(new WebSocketAdapter() { | |
@Override public void onTextMessage(WebSocket websocket, String message) throws Exception { | |
textView_received.setText(message); | |
Log.i(String.valueOf(websocket), message); | |
} | |
@Override public void onConnected(WebSocket websocket, Map<String, List<String>> headers) throws Exception { | |
super.onConnected(websocket, headers); | |
Log.d("WebSocketAdapter", "ON CONNECTED CALLED!"); | |
} | |
@Override public void onConnectError(WebSocket websocket, WebSocketException exception) throws Exception { | |
Log.e("WebSocketAdapter", "ON onConnectError CALLED!" + exception.getMessage()); | |
super.onConnectError(websocket, exception); | |
} | |
@Override public void onTextFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { | |
Log.d("WebSocketAdapter", "ON onTextFrame CALLED!"); | |
super.onTextFrame(websocket, frame); | |
} | |
@Override public void onPingFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { | |
Log.d("WebSocketAdapter", "ON onPingFrame CALLED!"); | |
super.onPingFrame(websocket, frame); | |
} | |
@Override public void onPongFrame(WebSocket websocket, WebSocketFrame frame) throws Exception { | |
Log.d("WebSocketAdapter", "ON onPongFrame CALLED!"); | |
super.onPongFrame(websocket, frame); | |
} | |
@Override public void onBinaryMessage(WebSocket websocket, byte[] binary) throws Exception { | |
Log.d("WebSocketAdapter", "ON onBinaryMessage CALLED!"); | |
super.onBinaryMessage(websocket, binary); | |
} | |
@Override public void onMessageError(WebSocket websocket, WebSocketException cause, List<WebSocketFrame> frames) throws Exception { | |
Log.d("WebSocketAdapter", "ON onMessageError CALLED!" + cause.getMessage()); | |
super.onMessageError(websocket, cause, frames); | |
} | |
@Override public void onFrameError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws Exception { | |
Log.d("WebSocketAdapter", "ON onFrameError CALLED!" + cause.getMessage()); | |
super.onFrameError(websocket, cause, frame); | |
} | |
@Override public void onTextMessageError(WebSocket websocket, WebSocketException cause, byte[] data) throws Exception { | |
Log.d("WebSocketAdapter", "ON onTextMessageError CALLED!" + cause.getMessage()); | |
super.onTextMessageError(websocket, cause, data); | |
} | |
@Override public void onSendError(WebSocket websocket, WebSocketException cause, WebSocketFrame frame) throws Exception { | |
Log.d("WebSocketAdapter", "ON onSendError CALLED!" + cause.getMessage()); | |
super.onSendError(websocket, cause, frame); | |
} | |
@Override public void onUnexpectedError(WebSocket websocket, WebSocketException cause) throws Exception { | |
Log.d("WebSocketAdapter", "ON onUnexpectedError CALLED! " + cause.getMessage()); | |
super.onUnexpectedError(websocket, cause); | |
} | |
@Override public void handleCallbackError(WebSocket websocket, Throwable cause) throws Exception { | |
Log.d("WebSocketAdapter", "ON CONNECTED CALLED!"); | |
super.handleCallbackError(websocket, cause); | |
} | |
@Override public void onSendingHandshake(WebSocket websocket, String requestLine, List<String[]> headers) throws Exception { | |
Log.d("WebSocketAdapter", "ON onSendingHandshake CALLED!" + requestLine + String.valueOf(headers)); | |
super.onSendingHandshake(websocket, requestLine, headers); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment