| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- package com.uas.esop.util;
- import android.annotation.SuppressLint;
- import android.util.Log;
- import java.io.BufferedReader;
- import java.io.DataInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.PrintWriter;
- import java.net.Socket;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- import java.util.concurrent.TimeUnit;
- import io.reactivex.rxjava3.core.Observable;
- import io.reactivex.rxjava3.disposables.Disposable;
- /**
- * @author qiaohao
- * @date 21-7-15 下午8:02
- */
- public class SocketUtils {
- private static final String TAG = "MainActivity:SocketUtils" ;
- private ExecutorService mExecutorService = Executors.newCachedThreadPool();
- public static final int INIT_CODE = 0x10;
- public static final int RESP_CODE = 0x11;
- public static final int NOT_CONNECTED_CODE = 0x12;
- private Socket mSocket;
- private InputStream inputStream;
- private DataInputStream input;
- private OutputStream outputStream;
- private SocketCallback mSocketCallback;
- private String deviceId;
- private Disposable subscribe;
- private static class Inner {
- private static final SocketUtils instance = new SocketUtils();
- }
- public static SocketUtils getInstance() {
- return Inner.instance;
- }
- private SocketUtils() {
- }
- public String getDeviceId(){
- return deviceId;
- }
- @SuppressLint("LongLogTag")
- public void initSocket(String ip, String port, SocketCallback socketCallback, String deviceId) {
- mSocketCallback = socketCallback;
- this.deviceId = deviceId;
- mExecutorService.execute(() ->{
- try {
- Log.e(TAG, "initSocket: "+ ip +":"+port);
- mSocket = new Socket(ip, Integer.parseInt(port));
- mSocket.setKeepAlive(true);
- mSocketCallback.call(INIT_CODE, mSocket.isConnected());
- Log.e(TAG, "initSocket: "+mSocket.isConnected());
- if (!mSocket.isConnected()){
- return;
- }
- // send2Server("hello!!");
- // startTimer();
- inputStream = mSocket.getInputStream();
- InputStreamReader isr = new InputStreamReader(inputStream);
- BufferedReader br = new BufferedReader(isr);
- String data = null;
- //读取客户端数据
- while((data = br.readLine()) != null){
- mSocketCallback.call(RESP_CODE, data);
- }
- //关闭输入流
- // mSocket.shutdownInput();
- } catch (Exception e) {
- e.printStackTrace();
- mSocketCallback.call(NOT_CONNECTED_CODE, e.getLocalizedMessage());
- }
- });
- }
- @SuppressLint("LongLogTag")
- private void startTimer(){
- stopTime();
- subscribe = Observable.interval(1, TimeUnit.SECONDS).subscribe((Long aLong) -> {
- send2Server("testData:"+aLong+"");
- Log.e(TAG, "startTimer: "+"testData:"+aLong+"" );
- });
- }
- private void stopTime(){
- if (subscribe!=null){
- subscribe.dispose();
- subscribe=null;
- }
- }
- public void send2Server(String content){
- mExecutorService.execute(()->{
- if (mSocket==null || !mSocket.isConnected()){
- mSocketCallback.call(NOT_CONNECTED_CODE, "服务未连接!");
- return;
- }
- try {
- outputStream = mSocket.getOutputStream();
- PrintWriter pw = new PrintWriter(outputStream);
- pw.write(content+"\n");
- pw.flush();
- //关闭输出流
- // mSocket.shutdownOutput();
- } catch (IOException e) {
- e.printStackTrace();
- }
- });
- }
- public void closeSocket(){
- try {
- stopTime();
- if (inputStream!=null){
- inputStream.close();
- inputStream = null;
- }
- if (input!=null){
- input.close();
- input = null;
- }
- if (mSocket!=null){
- mSocket.close();
- }
- Log.e("SocketUtils", "closeSocket: "+ mSocket.isConnected());
- if (mSocketCallback!=null){
- mSocketCallback.call(NOT_CONNECTED_CODE, "断开连接成功!");
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
|