1. 程式人生 > >AndroidAsync框架實現區域網通訊

AndroidAsync框架實現區域網通訊

package com.actiview.asynclib; import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.util.Log; import
com.koushikdutta.async.AsyncServer; import com.koushikdutta.async.AsyncServerSocket; import com.koushikdutta.async.AsyncSocket; import com.koushikdutta.async.ByteBufferList; import com.koushikdutta.async.DataEmitter; import com.koushikdutta.async.Util; import com.koushikdutta.async.callback.CompletedCallback; import
com.koushikdutta.async.callback.DataCallback; import com.koushikdutta.async.callback.ListenCallback; import com.koushikdutta.async.callback.WritableCallback; import com.koushikdutta.async.http.AsyncHttpClient; import com.koushikdutta.async.http.AsyncHttpGet; import com.koushikdutta.async.http.AsyncHttpResponse; import
com.koushikdutta.async.http.WebSocket; import com.koushikdutta.async.http.callback.HttpConnectCallback; import com.koushikdutta.async.http.server.AsyncHttpServer; import com.koushikdutta.async.http.server.AsyncHttpServerRequest; import com.koushikdutta.async.http.server.AsyncHttpServerResponse; import com.koushikdutta.async.http.server.HttpServerRequestCallback; import com.koushikdutta.async.util.Charsets; import java.io.BufferedOutputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.Enumeration; /** * 非同步網路框架工具類 * Created by xubaolun on 2018/7/26. */ public class AsynUtils { private static final String TAG="AsynUtils"; private static final int PORT=15234; public static String path=Environment.getExternalStorageDirectory().toString(); private static final String FILE_FLAG="/fileTrans"; private AsynUtils() { } static AsyncHttpServer asyncHttpServer=new AsyncHttpServer(); static AsyncServer asyncServer=new AsyncServer(); private static Handler handler=new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { return false; } }); public static void startServer(){ asyncServer.listen(null, PORT, new ListenCallback() { @Override public void onAccepted(final AsyncSocket socket) { Log.i(TAG,"------------startServer--------onAccepted--------------------"); socket.setDataCallback(new DataCallback() { @Override public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) { Log.i(TAG,"------------startServer--------onAccepted--------------------"+bb.readString(Charsets.UTF_8)); final String order="響應測試資訊"; Util.writeAll(socket, order.getBytes(), new CompletedCallback() { @Override public void onCompleted(Exception ex) { if (ex!=null) { Log.d("Socket", "writeAll出錯"); return; } Log.d("Socket", "傳送了:"+order); } }); } }); } @Override public void onListening(AsyncServerSocket socket) { Log.i(TAG,"------------startServer--------onListening--------------------"); } @Override public void onCompleted(Exception ex) { Log.i(TAG,"------------startServer--------onCompleted--------------------"); } }); } public static void startClient(){ AsyncServer.getDefault().connectSocket("10.0.0.9", PORT, new com.koushikdutta.async.callback.ConnectCallback() { @Override public void onConnectCompleted(Exception ex, AsyncSocket socket) { Log.i(TAG,"------------startClient--------onConnectCompleted--------------------"); if(ex!=null){ Log.d("Socket", "連接出錯"); ex.printStackTrace(); return; } socket.setDataCallback(new DataCallback() { @Override public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) { Log.d("Socket", "接收到:" + new String(bb.getAllByteArray())); } }); socket.setClosedCallback(new CompletedCallback() { @Override public void onCompleted(Exception ex) { if (ex!=null) { Log.d("Socket", "setClosedCallback出錯"); return; } Log.d("Socket", "setClosedCallback"); } }); socket.setEndCallback(new CompletedCallback() { @Override public void onCompleted(Exception ex) { if (ex!=null) { Log.d("Socket", "setEndCallback出錯"); return; } Log.d("Socket", "setEndCallback"); } }); socket.setWriteableCallback(new WritableCallback() { @Override public void onWriteable() { Log.d("Socket", "onWriteable"); } }); final String order="測試資訊"; Util.writeAll(socket, order.getBytes(), new CompletedCallback() { @Override public void onCompleted(Exception ex) { if (ex!=null) { Log.d("Socket", "writeAll出錯"); return; } Log.d("Socket", "傳送了:"+order); } }); } }); } public static void startHttpServerWebSocket(){ asyncHttpServer.listen(PORT); asyncHttpServer.setErrorCallback(new CompletedCallback() { @Override public void onCompleted(Exception ex) { Log.i(TAG,"----------------startHttpServerWebSocket------setErrorCallback---onCompleted-"); ex.printStackTrace(); } }); asyncHttpServer.websocket("/", new AsyncHttpServer.WebSocketRequestCallback() { @Override public void onConnected(final WebSocket webSocket, AsyncHttpServerRequest request) { Log.i(TAG,"----------------startHttpServerWebSocket------websocket---onConnected-"); webSocket.setDataCallback(new DataCallback() { @Override public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) { Log.i(TAG,"------------startHttpServerWebSocket--------onDataAvailable--------------------"+bb.readString(Charsets.UTF_8)); final String order="響應測試資訊"; Util.writeAll(webSocket, order.getBytes(), new CompletedCallback() { @Override public void onCompleted(Exception ex) { if (ex!=null) { Log.d("Socket", "writeAll出錯"); return; } Log.d("Socket", "傳送了:"+order); } }); } }); } }); } public static void startFileServerWebSocket(){ asyncHttpServer.listen(PORT); asyncHttpServer.setErrorCallback(new CompletedCallback() { @Override public void onCompleted(Exception ex) { Log.i(TAG,"----------------startHttpServerWebSocket------setErrorCallback---onCompleted-"); ex.printStackTrace(); } }); asyncHttpServer.websocket(FILE_FLAG, new AsyncHttpServer.WebSocketRequestCallback() { @Override public void onConnected(final WebSocket webSocket, AsyncHttpServerRequest request) { Log.i(TAG,"----------------startHttpServerWebSocket------websocket---onConnected-"); webSocket.setDataCallback(new DataCallback() { @Override public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) { Log.i(TAG,"------------startHttpServerWebSocket--------onDataAvailable--------------------"+bb.readString(Charsets.UTF_8)); final String order="響應測試資訊"; String path= Environment.getExternalStorageDirectory().getAbsolutePath()+Environment.getExternalStorageDirectory().getAbsolutePath(); Log.i("path","----------------path---"+path); final String fileName="download.jpg"; File file=new File(path,fileName); Util.writeAll(webSocket,getBytes(file.getAbsolutePath()) , new CompletedCallback() { @Override public void onCompleted(Exception ex) { if (ex!=null) { Log.d("Socket", "writeAll出錯"); return; } Log.d("Socket", "傳送檔案"); } }); } }); } }); } private static byte[] getBytes(String filePath){ byte[] buffer = null; try { File file = new File(filePath); FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); byte[] b = new byte[1000]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } public static void startHttpClientWebSocket(){ AsyncHttpClient.getDefaultInstance().websocket("ws://10.0.0.9:"+PORT,null, new AsyncHttpClient.WebSocketConnectCallback() { @Override public void onCompleted(Exception ex, WebSocket webSocket) { Log.i(TAG,"----------------startHttpClientWebSocket------websocket---onCompleted-"+webSocket); if(ex!=null){ ex.printStackTrace(); return; } webSocket.setDataCallback(new DataCallback() { @Override public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) { Log.d("Socket", "接收到:" + new String(bb.getAllByteArray())); } }); webSocket.setClosedCallback(new CompletedCallback() { @Override public void onCompleted(Exception ex) { if (ex!=null) { Log.d("Socket", "setClosedCallback出錯"); return; } Log.d("Socket", "setClosedCallback"); } }); webSocket.setEndCallback(new CompletedCallback() { @Override public void onCompleted(Exception ex) { if (ex!=null) { Log.d("Socket", "setEndCallback出錯"); return; } Log.d("Socket", "setEndCallback"); } }); webSocket.setWriteableCallback(new WritableCallback() { @Override public void onWriteable() { Log.d("Socket", "onWriteable"); } }); final String order="測試資訊"; Util.writeAll(webSocket, order.getBytes(), new CompletedCallback() { @Override public void onCompleted(Exception ex) { if (ex!=null) { Log.d("Socket", "writeAll出錯"); return; } Log.d("Socket", "傳送了:"+order); } }); } }); } public static void startFileClientWebSocket(final OnFileListener fileListener){ AsyncHttpClient.getDefaultInstance().websocket("ws://10.0.0.9:"+PORT+FILE_FLAG,null, new AsyncHttpClient.WebSocketConnectCallback() { @Override public void onCompleted(Exception ex, WebSocket webSocket) { Log.i(TAG,"----------------startHttpClientWebSocket------websocket---onCompleted-"+webSocket); if(ex!=null){ ex.printStackTrace(); return; } webSocket.setDataCallback(new DataCallback() { @Override public void onDataAvailable(DataEmitter emitter, ByteBufferList bb) { Log.d("Socket", "接收到檔案" ); getFile(bb.getAllByteArray(),path,"hello.jpg"); Log.d("Socket", "接收到檔案完畢" ); fileListener.onSuccess(path+"/"+"hello.jpg"); } }); webSocket.setClosedCallback(new CompletedCallback() { @Override public void onCompleted(Exception ex) { if (ex!=null) { Log.d("Socket", "setClosedCallback出錯"); return; } Log.d("Socket", "setClosedCallback"); } }); webSocket.setEndCallback(new CompletedCallback() { @Override public void onCompleted(Exception ex) { if (ex!=null) { Log.d("Socket", "setEndCallback出錯"); return; } Log.d("Socket", "setEndCallback"); } }); webSocket.setWriteableCallback(new WritableCallback() { @Override public void onWriteable() { Log.d("Socket", "onWriteable"); } }); final String order="測試資訊"; Util.writeAll(webSocket, order.getBytes(), new CompletedCallback() { @Override public void onCompleted(Exception ex) { if (ex!=null) { Log.d("Socket", "writeAll出錯"); return; } Log.d("Socket", "傳送了:"+order); } }); } }); } /** * 根據byte陣列,生成檔案 */ public static void getFile(byte[] bfile, String filePath,String fileName) { BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { File dir = new File(filePath); if(!dir.exists()&&dir.isDirectory()){//判斷檔案目錄是否存在 dir.mkdirs(); } file = new File(filePath+"/"+fileName); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bfile); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } public static void startHttpServer(){ asyncHttpServer.listen(PORT); asyncHttpServer.setErrorCallback(new CompletedCallback() { @Override public void onCompleted(Exception ex) { Log.i(TAG,"----------------startHttpServer------setErrorCallback---onCompleted-"); ex.printStackTrace(); } }); asyncHttpServer.get("/", new HttpServerRequestCallback() { @Override public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) { Log.i(TAG,"----------------startHttpServer------get---onRequest-----"); } }); asyncHttpServer.post("/", new HttpServerRequestCallback() { @Override public void onRequest(AsyncHttpServerRequest request, AsyncHttpServerResponse response) { Log.i(TAG,"----------------startHttpServer------post---onRequest-"); } }); asyncHttpServer.get("/wf_files/.*", new HttpServerRequestCallback() { @Override public void onRequest(final AsyncHttpServerRequest request, final AsyncHttpServerResponse response) { } }); } public static void startHttpClient(){ AsyncHttpClient.getDefaultInstance().execute("http://10.0.0.9:" + PORT, new HttpConnectCallback() { @Override public void onConnectCompleted(Exception ex, AsyncHttpResponse response) { } }); } public static void getFile(String uri, String fileName, AsyncHttpClient.FileCallback fileCallback){ AsyncHttpClient.getDefaultInstance().executeFile(new AsyncHttpGet(uri),fileName,fileCallback); } public static void getString(String uri){ AsyncHttpClient.getDefaultInstance().executeString(new AsyncHttpGet(uri), new AsyncHttpClient.StringCallback() { @Override public void onCompleted(Exception e, AsyncHttpResponse source, String result) { } }); } public static String getIPAddress(Context context) { NetworkInfo info = ((ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); if (info != null && info.isConnected()) { if (info.getType() == ConnectivityManager.TYPE_MOBILE) {//當前使用2G/3G/4G網路 try { //Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces(); for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) { NetworkInterface intf = en.nextElement(); for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { return inetAddress.getHostAddress(); } } } } catch (SocketException e) { e.printStackTrace(); } } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//當前使用無線網路 WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());//得到IPV4地址 return ipAddress; } } else { //當前無網路連線,請在設定中開啟網路 } return null; } /** * 將得到的int型別的IP轉換為String型別 * * @param ip * @return */ public static String intIP2StringIP(int ip) { return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + (ip >> 24 & 0xFF); } public interface OnFileListener{ void onSuccess(String file); } }