From cc44e0f8151fe2b616d6dd59c7932c17b1bc525b Mon Sep 17 00:00:00 2001 From: zmt Date: Fri, 20 May 2022 14:31:04 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/io/dcloud/zmt_module/AsyncTask.java | 56 ----- .../io/dcloud/zmt_module/MyCallbackInterface.java | 12 -- .../main/java/io/dcloud/zmt_module/MyThread.java | 43 ---- .../java/io/dcloud/zmt_module/SocketConfig.java | 8 - .../java/io/dcloud/zmt_module/SocketHelper.java | 54 ----- .../main/java/io/dcloud/zmt_module/TcpServer.java | 196 ------------------ .../zmt_module/tcpServer/MyCallbackInterface.java | 20 ++ .../io/dcloud/zmt_module/tcpServer/MyThread.java | 46 +++++ .../dcloud/zmt_module/tcpServer/SocketConfig.java | 8 + .../dcloud/zmt_module/tcpServer/SocketHelper.java | 74 +++++++ .../io/dcloud/zmt_module/tcpServer/TCPTask.java | 70 +++++++ .../io/dcloud/zmt_module/tcpServer/TcpServer.java | 229 +++++++++++++++++++++ .../main/java/io/dcloud/zmt_module/zmtClass.java | 123 ++--------- 13 files changed, 467 insertions(+), 472 deletions(-) delete mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/AsyncTask.java delete mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/MyCallbackInterface.java delete mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/MyThread.java delete mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/SocketConfig.java delete mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/SocketHelper.java delete mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/TcpServer.java create mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyCallbackInterface.java create mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyThread.java create mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketConfig.java create mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketHelper.java create mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TCPTask.java create mode 100644 zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TcpServer.java diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/AsyncTask.java b/zmt_module/src/main/java/io/dcloud/zmt_module/AsyncTask.java deleted file mode 100644 index e17ed84..0000000 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/AsyncTask.java +++ /dev/null @@ -1,56 +0,0 @@ -package io.dcloud.zmt_module; - -import com.alibaba.fastjson.JSONObject; - -import io.dcloud.feature.uniapp.AbsSDKInstance; - -public class AsyncTask { - - String TAG = "zmt_module_AsyncTask "; - - public interface MyTcpCallback{ - void tcpCallback(Object object); - } - -// TCP task - public void task(final MyTcpCallback myCallback, final Integer port){ - // Android 4.0 之后不能在主线程中请求HTTP请求 - Thread thread = new Thread(new Runnable() { - @Override - public void run() { - System.out.println("AsyncTask thread running... port:"+port); - - TcpServer server = new TcpServer(); -// server.initSocket((Integer.parseInt("16666"))); - server.initSocket(port); - - while (true){ - String tcp_receive_message = server.receiveMessage(SocketConfig.UTF_8);//接收消息 - System.out.println(TAG+"接收消息:"+tcp_receive_message); - String send_msg = "App received tcp message!"; //回发socket消息,确认接收 - send_msg = SocketHelper.getMessageByReadLine(send_msg); - server.sendMessage(send_msg,SocketConfig.UTF_8);//回发消息 - - JSONObject data = new JSONObject(); - data.put("threadID", Thread.currentThread().getId()); - data.put("threadName", Thread.currentThread().getName()); -// data.put("message", server.); -// data.put("message", tcp_receive_message); - data.put("message", tcp_receive_message); - myCallback.tcpCallback(data);//回调到zmtClass给uni端 - - try { - Thread.sleep(1000); //定义休眠时间 - } catch (InterruptedException e) { - e.printStackTrace(); - } - - } - - } - }); - thread.setDaemon(true);//开启守护线程 - thread.start(); - } - -} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/MyCallbackInterface.java b/zmt_module/src/main/java/io/dcloud/zmt_module/MyCallbackInterface.java deleted file mode 100644 index 5ecbcc8..0000000 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/MyCallbackInterface.java +++ /dev/null @@ -1,12 +0,0 @@ -package io.dcloud.zmt_module; - -public class MyCallbackInterface { - - public interface MyTcpCallback{ - void callback(Object object); - } - - public interface UniCallback{ - void callback(Object object); - } -} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/MyThread.java b/zmt_module/src/main/java/io/dcloud/zmt_module/MyThread.java deleted file mode 100644 index 884b780..0000000 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/MyThread.java +++ /dev/null @@ -1,43 +0,0 @@ -package io.dcloud.zmt_module; - -public class MyThread extends Thread { - String TAG = "zmt---"; - public void run(){ - System.out.println("MyThread thread running..."); - - TcpServer server = new TcpServer(); - server.initSocket((Integer.parseInt("16666"))); - - while (true){ - String message = server.receiveMessage(SocketConfig.UTF_8); - System.out.println(TAG+"接收消息:"+message); - - String send_msg = "收到消息了,你说的对!"; - - send_msg = SocketHelper.getMessageByReadLine(send_msg); - server.sendMessage(send_msg,SocketConfig.UTF_8); - try { - Thread.sleep(1000); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - } - -// while (true){ -// String message = server.receiveMessage(SocketConfig.UTF_8); -// System.out.println(TAG+"接收消息:"+message); -// -// String send_msg = "101010"; -// -// send_msg = SocketHelper.getMessageByReadLine(send_msg); -// server.sendMessage(send_msg,SocketConfig.UTF_8); -// try { -// Thread.sleep(1000); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// -// } - } -} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/SocketConfig.java b/zmt_module/src/main/java/io/dcloud/zmt_module/SocketConfig.java deleted file mode 100644 index d22f614..0000000 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/SocketConfig.java +++ /dev/null @@ -1,8 +0,0 @@ -package io.dcloud.zmt_module; - -public class SocketConfig { - -// 设置编码 - public static final String UTF_8 = "UTF-8"; - public static final String GBK = "GBK"; -} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/SocketHelper.java b/zmt_module/src/main/java/io/dcloud/zmt_module/SocketHelper.java deleted file mode 100644 index 425a5b3..0000000 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/SocketHelper.java +++ /dev/null @@ -1,54 +0,0 @@ -package io.dcloud.zmt_module; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.util.Arrays; - -public class SocketHelper { - public static String getMessageByReadLine(String message) { - message = message + "\n"; - return message; - } - - public static byte[] toByteArray(InputStream in) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] buffer = new byte[1024 * 4]; - int n = 0; - while ((n = in.read(buffer)) != -1) { - out.write(buffer, 0, n); - } - return out.toByteArray(); - } - - public static byte[] toIntByteArray(InputStream in) throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - byte[] buffer = new byte[100]; - System.out.println("zmt-toIntByteArray--" + buffer[0] + buffer[1] + buffer[2] + buffer[3] + buffer[4] + buffer[5] + " " + Arrays.toString(buffer) - ); -// +new String(buffer.array() - int n = 0; - while ((n = in.read(buffer)) != -1) { - out.write(buffer, 0, n); - } - - - return out.toByteArray(); - } - - public static String byte2hex(byte[] bytes) { - StringBuilder sb = new StringBuilder(); - String tmp = null; - for (byte b : bytes) { - //将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为16进制 - tmp = Integer.toHexString(0xFF & b); - if (tmp.length() == 1) { - tmp = "0" + tmp; - } - sb.append(tmp); - - } - return sb.toString(); - } - -} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/TcpServer.java b/zmt_module/src/main/java/io/dcloud/zmt_module/TcpServer.java deleted file mode 100644 index 47e8bc0..0000000 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/TcpServer.java +++ /dev/null @@ -1,196 +0,0 @@ -package io.dcloud.zmt_module; - -import android.content.Context; -import android.net.ConnectivityManager; -import android.net.NetworkInfo; - -import java.io.BufferedInputStream; -import java.io.BufferedReader; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.OutputStream; -import java.io.OutputStreamWriter; -import java.io.PrintStream; -import java.io.PrintWriter; -import java.net.Inet4Address; -import java.net.InetAddress; -import java.net.NetworkInterface; -import java.net.ServerSocket; -import java.net.Socket; -import java.net.SocketException; -import java.nio.charset.Charset; -import java.util.Arrays; -import java.util.Enumeration; - -public class TcpServer { - // 私有变量 - private String TAG = "zmt---"; - private ServerSocket mServerSocket; - // private DatagramSocket mServerSocket; - private Socket mServer; - - private OutputStream mOutStream; - private PrintWriter mPrinter; - - private InputStream mInputStream; - private BufferedInputStream mBufferedInputStream; - private InputStreamReader mInputStreamReader; - private BufferedReader mBufferedReader; - - /** - *使用本机ip 建立socket server - * @param port 端口号:0-65535 - * - */ - public void initSocket(int port){ - System.out.println(TAG+"initSocket"); - try{ - if (mServerSocket != null) { mServerSocket.close(); mServerSocket = null; } - - if (mServerSocket==null){ - mServerSocket = new ServerSocket(port); - mServerSocket.setReuseAddress(true); - -// mServerSocket.bind(new InetSocketAddress(port)); - InetAddress inetAddress = InetAddress.getLocalHost(); //本机地址 - System.out.println(TAG+"tcp server 建立完成:host name"+inetAddress.getHostName()+"\n ---host address:" + - inetAddress.getHostAddress()+" , address:"+inetAddress.getAddress().toString()+" 端口:"+port+ "----"+getIPAddressForNetwork()); - } - - -// if(mServerSocket==null){ -// //自己IP收信 -//// IPAddress ip = IPAddress.Parse("192.168.1.96"); -// -//// String ip = "192.168.1.96"; -// InetAddress ip = InetAddress.getByName("192.168.1.96"); -// -// mServerSocket = new ServerSocket(port); -// mServerSocket.bind(new IPEndPoint(ip,port)); -// -// Socket mServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); -// m_serverListenSocket.Bind(new IPEndPoint(ip, 10035)); -// m_serverListenSocket.Listen(100); -// } - }catch (IOException e){ - e.printStackTrace(); - } - } - - public interface MySocketCallback{ - void callback(Object object); - } - - private String getIPAddressForNetwork() throws SocketException { - String address = null; - for(Enumeration enum1 = NetworkInterface.getNetworkInterfaces(); enum1.hasMoreElements();){ - NetworkInterface networkInterface = enum1.nextElement(); - for(Enumeration enum2=networkInterface.getInetAddresses(); enum2.hasMoreElements();){ - InetAddress inetAddress = enum2.nextElement(); - if(!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address){ - address = "type: 手机网络\n本机IP: " + inetAddress.getHostAddress().toString(); - } - } - } - return address; - } - - // 接收消息 - public String receiveMessage(String charsetName){ - String receiveData=null; - String readLine=null; - -// if(mServerSocket==null){ -// System.out.println(TAG+"Socket 未启动"); -// return null; -// } - System.out.println(TAG+"tcp server receiveMessage ing..."); - try{ - System.out.println("mServerSocket:"+mServerSocket.toString()); -// 获取输入信息 -// if(mServerSocket==null){ -// System.out.println("mServerSocket 未初始化,无法receiveMessage"); -// return null; -// } - mServer = mServerSocket.accept(); - mInputStream = mServer.getInputStream(); - - byte[] data = SocketHelper.toByteArray(mInputStream); - -// String _rs = new String(data, "gbk"); - -// System.out.println(TAG+"二进制读取结果:"+data+" - "+_rs +" - "+SocketHelper.toIntByteArray(mInputStream) + Arrays.toString(data)) ; - - System.out.println(TAG+SocketHelper.byte2hex(data)); - receiveData = SocketHelper.byte2hex(data); -// 读取输入流 -// charsetName = null; -// if(charsetName!=null&&charsetName!=""){ -// mInputStreamReader = new InputStreamReader(mInputStream, Charset.forName(charsetName)); -// }else{ -// mInputStreamReader = new InputStreamReader(mInputStream); -// } - -// mBufferedReader = new BufferedReader(mInputStreamReader); -// readLine = mBufferedReader.readLine(); -// while (readLine!=null&&readLine!=""){ -// System.out.println(TAG+"读取结果:"+readLine); -// break; -// } - System.out.println(TAG+"接收结束,数据:"+receiveData); - - } catch (IOException e) { - e.printStackTrace(); - } - return receiveData; - - } - - // 发送消息 - public void sendMessage(String message,String charsetName){ - if(mServer != null){ - System.out.println(TAG+"sendMessage ing..."); - - try { - mOutStream = mServer.getOutputStream(); - mPrinter = new PrintWriter(new OutputStreamWriter(mOutStream,Charset.forName(charsetName))); - mPrinter.println(message); - mPrinter.flush(); - - System.out.println(TAG+"发送成功 :"+message); - } catch (IOException e) { - System.out.println(TAG+"tcp server sendMessage err :"+message); - e.printStackTrace(); - } - - } - } - - // 关闭连接 - public void close(){ - try { - if (mPrinter!=null){ - mPrinter.close(); - } - if (mOutStream!=null){ - mOutStream.close(); - } - if (mInputStream!=null){ - mInputStream.close(); - } - if (mInputStreamReader!=null){ - mInputStreamReader.close(); - } - if (mBufferedReader!=null){ - mBufferedReader.close(); - } - if (mServer!=null){ - mServer.close(); - } - System.out.println(TAG+"关闭tcp server <------"); - } catch (IOException e) { - e.printStackTrace(); - } - } -} \ No newline at end of file diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyCallbackInterface.java b/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyCallbackInterface.java new file mode 100644 index 0000000..fc0a669 --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyCallbackInterface.java @@ -0,0 +1,20 @@ +package io.dcloud.zmt_module; + +public class MyCallbackInterface { + + public interface TcpCallback{ + void tcpCallback(Object object); + } + + public interface SocketInitCallback{ + void socketInitCallback(Object object); + } + + public interface UniCallback{ + void callback(Object object); + } + + public interface TestCallback{ + void tcallback(Object object); + } +} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyThread.java b/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyThread.java new file mode 100644 index 0000000..2e25db8 --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyThread.java @@ -0,0 +1,46 @@ +package io.dcloud.zmt_module; + +import io.dcloud.zmt_module.tcpServer.SocketHelper; +import io.dcloud.zmt_module.tcpServer.TcpServer; + +public class MyThread extends Thread { + String TAG = "zmt---"; + public void run(){ + System.out.println("MyThread thread running..."); + + TcpServer server = new TcpServer(); +// server.initSocket((Integer.parseInt("16666"))); + + while (true){ + String message = server.receiveMessage(SocketConfig.UTF_8); + System.out.println(TAG+"接收消息:"+message); + + String send_msg = "收到消息了,你说的对!"; + + send_msg = SocketHelper.getMessageByReadLine(send_msg); + server.sendMessage(send_msg,SocketConfig.UTF_8); + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + +// while (true){ +// String message = server.receiveMessage(SocketConfig.UTF_8); +// System.out.println(TAG+"接收消息:"+message); +// +// String send_msg = "101010"; +// +// send_msg = SocketHelper.getMessageByReadLine(send_msg); +// server.sendMessage(send_msg,SocketConfig.UTF_8); +// try { +// Thread.sleep(1000); +// } catch (InterruptedException e) { +// e.printStackTrace(); +// } +// +// } + } +} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketConfig.java b/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketConfig.java new file mode 100644 index 0000000..d22f614 --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketConfig.java @@ -0,0 +1,8 @@ +package io.dcloud.zmt_module; + +public class SocketConfig { + +// 设置编码 + public static final String UTF_8 = "UTF-8"; + public static final String GBK = "GBK"; +} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketHelper.java b/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketHelper.java new file mode 100644 index 0000000..1371faf --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketHelper.java @@ -0,0 +1,74 @@ +package io.dcloud.zmt_module.tcpServer; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Arrays; +import java.util.Enumeration; + +public class SocketHelper { + public static String getMessageByReadLine(String message) { + message = message + "\n"; + return message; + } + + public static byte[] toByteArray(InputStream in) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024 * 4]; + int n = 0; + while ((n = in.read(buffer)) != -1) { + out.write(buffer, 0, n); + } + return out.toByteArray(); + } + + public static byte[] toIntByteArray(InputStream in) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + byte[] buffer = new byte[100]; + System.out.println("zmt-toIntByteArray--" + buffer[0] + buffer[1] + buffer[2] + buffer[3] + buffer[4] + buffer[5] + " " + Arrays.toString(buffer) + ); +// +new String(buffer.array() + int n = 0; + while ((n = in.read(buffer)) != -1) { + out.write(buffer, 0, n); + } + + + return out.toByteArray(); + } + + public static String byte2hex(byte[] bytes) { + StringBuilder sb = new StringBuilder(); + String tmp = null; + for (byte b : bytes) { + //将每个字节与0xFF进行与运算,然后转化为10进制,然后借助于Integer再转化为16进制 + tmp = Integer.toHexString(0xFF & b); + if (tmp.length() == 1) { + tmp = "0" + tmp; + } + sb.append(tmp); + + } + return sb.toString(); + } + + public static String getIPAddressForNetwork() throws SocketException { + String address = null; + for(Enumeration enum1 = NetworkInterface.getNetworkInterfaces(); enum1.hasMoreElements();){ + NetworkInterface networkInterface = enum1.nextElement(); + for(Enumeration enum2 = networkInterface.getInetAddresses(); enum2.hasMoreElements();){ + InetAddress inetAddress = enum2.nextElement(); + if(!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address){ +// address = "type: 手机网络\n本机IP: " + inetAddress.getHostAddress().toString(); + address = inetAddress.getHostAddress().toString(); + } + } + } + return address; + } + +} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TCPTask.java b/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TCPTask.java new file mode 100644 index 0000000..21606da --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TCPTask.java @@ -0,0 +1,70 @@ +package io.dcloud.zmt_module.tcpServer; + +import com.alibaba.fastjson.JSONObject; + +import io.dcloud.feature.uniapp.bridge.UniJSCallback; +import io.dcloud.zmt_module.SocketConfig; +import io.dcloud.zmt_module.tcpServer.SocketHelper; +import io.dcloud.zmt_module.tcpServer.TcpServer; + +public class TCPTask { + + String TAG = "zmt_module_AsyncTask "; + + public interface MyTcpCallback{ + void tcpCallback(Object object); + } + + TcpServer server = new TcpServer(); + Thread myThread; +// TCP task + public void task(final Integer port, final UniJSCallback uniJSCallback){ + // Android 4.0 之后不能在主线程中请求HTTP请求 +// System.out.println("AsyncTask thread running... ==>>>44444444:"+port+ server.isServerBound()); + + myThread = new Thread(new Runnable() { + @Override + public void run() { + System.out.println("AsyncTask thread running... port:"+port+ server); +// TcpServer server = new TcpServer(); + +// server = new TcpServer(); +// server.initSocket((Integer.parseInt("16666"))); + server.initSocket(port,uniJSCallback); + + while (true){ + String tcp_receive_message = server.receiveMessage(SocketConfig.UTF_8);//接收消息 + + if(tcp_receive_message!=null){ + System.out.println(TAG+"接收消息:"+tcp_receive_message); + String send_msg = "App received tcp message!"; //回发socket消息,确认接收 + send_msg = SocketHelper.getMessageByReadLine(send_msg); + server.sendMessage(send_msg, SocketConfig.UTF_8);//回发消息 + + JSONObject data = new JSONObject(); + data.put("threadID", Thread.currentThread().getId()); + data.put("threadName", Thread.currentThread().getName()); +// data.put("message", server.); +// data.put("message", tcp_receive_message); + data.put("message", tcp_receive_message); +// myCallback.tcpCallback(data); +// myTcpCallback.tcpCallback(data);//回调到zmtClass给uni端 + + uniJSCallback.invokeAndKeepAlive(data); + } + + try { + Thread.sleep(1000); //定义休眠时间 + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + + } + }); + myThread.setDaemon(true);//开启守护线程 + myThread.start(); + } + +} diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TcpServer.java b/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TcpServer.java new file mode 100644 index 0000000..c516410 --- /dev/null +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TcpServer.java @@ -0,0 +1,229 @@ +package io.dcloud.zmt_module.tcpServer; + +import com.alibaba.fastjson.JSONObject; + +import java.io.BufferedInputStream; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.net.InetAddress; +import java.net.ServerSocket; +import java.net.Socket; +import java.nio.charset.Charset; + +import io.dcloud.feature.uniapp.bridge.UniJSCallback; +import io.dcloud.zmt_module.SocketConfig; +import io.dcloud.zmt_module.tcpServer.SocketHelper; + +public class TcpServer { + // 私有变量 + private String TAG = "zmt---"; + private ServerSocket mServerSocket; + // private DatagramSocket mServerSocket; + private Socket mServer; + + private OutputStream mOutStream; + private PrintWriter mPrinter; + + private InputStream mInputStream; + private BufferedInputStream mBufferedInputStream; + private InputStreamReader mInputStreamReader; + private BufferedReader mBufferedReader; + + /** + *使用本机ip 建立socket server + * @param port 端口号:0-65535 + * + */ + public void initSocket(int port, UniJSCallback uniJSCallback){ + System.out.println(TAG+"initSocket"); + try{ + if (mServerSocket != null) { mServerSocket.close(); mServerSocket = null; } + + if (mServerSocket==null){ + mServerSocket = new ServerSocket(port); + mServerSocket.setReuseAddress(true); + +// mServerSocket.bind(new InetSocketAddress(port)); + InetAddress inetAddress = InetAddress.getLocalHost(); //本机地址 + System.out.println(TAG+"tcp server 建立完成:host name"+inetAddress.getHostName()+"\n ---host address:" + + inetAddress.getHostAddress()+" , address:"+inetAddress.getAddress().toString()+" 端口:"+port+ "----"+ SocketHelper.getIPAddressForNetwork()); + + com.alibaba.fastjson.JSONObject data = new com.alibaba.fastjson.JSONObject(); + data.put("port", port); + data.put("ip", SocketHelper.getIPAddressForNetwork()); + uniJSCallback.invokeAndKeepAlive(data); + + + while (true){ + String tcp_receive_message = receiveMessage(SocketConfig.UTF_8);//接收消息 + + if(tcp_receive_message!=null){ + System.out.println(TAG+"接收消息:"+tcp_receive_message); + String send_msg = "App received tcp message!"; //回发socket消息,确认接收 + send_msg = SocketHelper.getMessageByReadLine(send_msg); + sendMessage(send_msg, SocketConfig.UTF_8);//回发消息 + + JSONObject data_receive = new JSONObject(); + data_receive.put("threadID", Thread.currentThread().getId()); + data_receive.put("threadName", Thread.currentThread().getName()); +// data.put("message", server.); +// data.put("message", tcp_receive_message); + data_receive.put("message", tcp_receive_message); +// myCallback.tcpCallback(data); +// myTcpCallback.tcpCallback(data);//回调到zmtClass给uni端 + + uniJSCallback.invokeAndKeepAlive(data_receive); + } + + try { + Thread.sleep(1000); //定义休眠时间 + } catch (InterruptedException e) { + e.printStackTrace(); + } + + } + } + + +// if(mServerSocket==null){ +// //自己IP收信 +//// IPAddress ip = IPAddress.Parse("192.168.1.96"); +// +//// String ip = "192.168.1.96"; +// InetAddress ip = InetAddress.getByName("192.168.1.96"); +// +// mServerSocket = new ServerSocket(port); +// mServerSocket.bind(new IPEndPoint(ip,port)); +// +// Socket mServerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); +// m_serverListenSocket.Bind(new IPEndPoint(ip, 10035)); +// m_serverListenSocket.Listen(100); +// } + }catch (IOException e){ + + if(e.getMessage().equals("bind failed: EADDRINUSE (Address already in use)")){ + System.out.println("已初始化过TCP,忽略该操作:"+e.getMessage()); + JSONObject _data = new JSONObject(); + _data.put("code",0); + _data.put("toast","已初始化过TCP,请勿重复执行初始化操作。"); + uniJSCallback.invokeAndKeepAlive(_data); + }else{ + e.printStackTrace(); + } + } + } + + public interface MySocketCallback{ + void callback(Object object); + } + + public Boolean isServerBound(){ + return mServerSocket.isBound(); + } + + // 接收消息 + public String receiveMessage(String charsetName){ + String receiveData=null; + String readLine=null; + +// if(mServerSocket==null){ +// System.out.println(TAG+"Socket 未启动"); +// return null; +// } + System.out.println(TAG+"tcp server receiveMessage ing..."); + try{ + if(mServerSocket==null){ + return null; + } + System.out.println("mServerSocket:"+mServerSocket.toString()); +// 获取输入信息 +// if(mServerSocket==null){ +// System.out.println("mServerSocket 未初始化,无法receiveMessage"); +// return null; +// } + mServer = mServerSocket.accept(); + mInputStream = mServer.getInputStream(); + + byte[] data = SocketHelper.toByteArray(mInputStream); + +// String _rs = new String(data, "gbk"); + +// System.out.println(TAG+"二进制读取结果:"+data+" - "+_rs +" - "+SocketHelper.toIntByteArray(mInputStream) + Arrays.toString(data)) ; + + System.out.println(TAG+SocketHelper.byte2hex(data)); + receiveData = SocketHelper.byte2hex(data); +// 读取输入流 +// charsetName = null; +// if(charsetName!=null&&charsetName!=""){ +// mInputStreamReader = new InputStreamReader(mInputStream, Charset.forName(charsetName)); +// }else{ +// mInputStreamReader = new InputStreamReader(mInputStream); +// } + +// mBufferedReader = new BufferedReader(mInputStreamReader); +// readLine = mBufferedReader.readLine(); +// while (readLine!=null&&readLine!=""){ +// System.out.println(TAG+"读取结果:"+readLine); +// break; +// } + System.out.println(TAG+"接收结束,数据:"+receiveData); + + } catch (IOException e) { + e.printStackTrace(); + } + return receiveData; + + } + + // 发送消息 + public void sendMessage(String message,String charsetName){ + if(mServer != null){ + System.out.println(TAG+"sendMessage ing..."); + + try { + mOutStream = mServer.getOutputStream(); + mPrinter = new PrintWriter(new OutputStreamWriter(mOutStream,Charset.forName(charsetName))); + mPrinter.println(message); + mPrinter.flush(); + + System.out.println(TAG+"发送成功 :"+message); + } catch (IOException e) { + System.out.println(TAG+"tcp server sendMessage err :"+message); + e.printStackTrace(); + } + + } + } + + // 关闭连接 + public void close(){ + try { + if (mPrinter!=null){ + mPrinter.close(); + } + if (mOutStream!=null){ + mOutStream.close(); + } + if (mInputStream!=null){ + mInputStream.close(); + } + if (mInputStreamReader!=null){ + mInputStreamReader.close(); + } + if (mBufferedReader!=null){ + mBufferedReader.close(); + } + if (mServer!=null){ + mServer.close(); + } + System.out.println(TAG+"关闭tcp server <------"); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java b/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java index 34355e9..7803cb2 100644 --- a/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java +++ b/zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java @@ -1,135 +1,52 @@ package io.dcloud.zmt_module; import com.alibaba.fastjson.JSONObject; -import com.taobao.weex.bridge.JSCallback; -import com.tencent.iot.hub.device.android.core.gateway.TXGatewayConnection; -import io.dcloud.feature.uniapp.AbsSDKInstance; -import io.dcloud.feature.uniapp.UniSDKInstance; import io.dcloud.feature.uniapp.annotation.UniJSMethod; import io.dcloud.feature.uniapp.bridge.UniJSCallback; import io.dcloud.feature.uniapp.common.UniModule; +import io.dcloud.zmt_module.tcpServer.TCPTask; import android.util.Log; -import java.util.HashMap; -import java.util.Map; +public class zmtClass extends UniModule { -public class zmtClass extends UniModule implements AsyncTask.MyTcpCallback{ - - String TAG = "zmt---"; - final AbsSDKInstance uniSDK = this.mUniSDKInstance;//定义uniSDK - public UniJSCallback uniTcpStartCallback; + String TAG = "module plugin"; +// final AbsSDKInstance uniSDK = this.mUniSDKInstance;//定义uniSDK //run ui thread @UniJSMethod(uiThread = true) public void tcpAsyncFunc(JSONObject options, final UniJSCallback callback) { - Log.e(TAG, "tcpAsyncFunc--zmt:"+options); - uniTcpStartCallback = callback; - - if(callback != null) { - JSONObject data = new JSONObject(); - data.put("msg", "已接收参数,处理中..."+options.toJSONString()); - callback.invokeAndKeepAlive(data); - - } + Log.e(TAG, "tcpAsyncFunc:"+options); Integer port = 16666;//默认端口,当uni传递port参数时,采用传递port初始化tcp String s_port = options.getString("port"); if (!s_port.isEmpty()){ - Log.e(TAG, "tcpAsyncFunc--port:"+s_port); + Log.e(TAG, "设置传入端口:"+s_port); port = Integer.parseInt(s_port); } - new AsyncTask().task(new AsyncTask.MyTcpCallback() { - @Override - public void tcpCallback(Object object) { - // Map params=new HashMap<>(); + //开启TCP,传入jsCallback + + new TCPTask().task(port, callback); + +// 全局回调 +// Map params=new HashMap<>(); // params.put("fireGlobalEventCallback","全局事件测试"); // uniSDK.fireGlobalEventCallback("myEvent", params);//发送全局参数直接回调 - callback.invokeAndKeepAlive(object);//使用jsCallback回调 - } - },port); - - // Android 4.0 之后不能在主线程中请求HTTP请求 -// new Thread(new Runnable(){ -// @Override -// public void run() { -// System.out.println("MyThread thread running..."); -// -// TcpServer server = new TcpServer(); -// server.initSocket((Integer.parseInt("16666"))); -// -// while (true){ -// String message = server.receiveMessage(SocketConfig.UTF_8); -// System.out.println(TAG+"接收消息:"+message); -// -// JSONObject data = new JSONObject(); -// data.put("code", message); -//// callback.invoke(data); -// callback.invokeAndKeepAlive(data); -// -// String send_msg = "1111111111111"; -// -// send_msg = SocketHelper.getMessageByReadLine(send_msg); -// server.sendMessage(send_msg,SocketConfig.UTF_8); -// try { -// Thread.sleep(1000); -// } catch (InterruptedException e) { -// e.printStackTrace(); -// } -// -// } -// -// } -// }).start(); - - } - public void callbackOnce(JSONObject data, JSCallback callback){ -// JSONObject data = new JSONObject(); -// data.put("code", object); - callback.invoke(data); } - @Override - public void tcpCallback(Object object) { - System.out.println("6666666"+object.toString()); - uniTcpStartCallback.invokeAndKeepAlive(object); - } + @UniJSMethod(uiThread = true) + public void testAsyncFunc(JSONObject options, UniJSCallback callback) { + Log.e(TAG, "testAsyncFunc--zmt:"+options); - //定义MyTcpCallback - public interface MyTcpCallback{ - void callback(Object object); + if(callback != null) { + JSONObject data = new JSONObject(); + data.put("code", "success-zmt async"); + callback.invoke(data); + } } -// public void task(final AsyncTask.MyCallback myCallback){ -// Thread thread = new Thread(new Runnable() { -// @Override -// public void run() { -// try{ -// Thread.sleep(3000); -// }catch (InterruptedException e){ -// e.printStackTrace(); -// } -// int sum = 0; -// for(int i=0;sum<99;sum++){ -// sum+=i; -// } -// myCallback.callback(sum); -// } -// }); -// thread.start(); -// } -// @UniJSMethod(uiThread = true) -// public void testAsyncFunc(JSONObject options, UniJSCallback callback) { -// Log.e(TAG, "testAsyncFunc--zmt:"+options); -// -// if(callback != null) { -// JSONObject data = new JSONObject(); -// data.put("code", "success-zmt async"); -// callback.invoke(data); -// } -// } //run JS thread @UniJSMethod(uiThread = false)