Browse Source

优化代码

module-tcpserver-mqtt-t4j-faceSDK
zmt 3 years ago
parent
commit
cc44e0f815
  1. 56
      zmt_module/src/main/java/io/dcloud/zmt_module/AsyncTask.java
  2. 12
      zmt_module/src/main/java/io/dcloud/zmt_module/MyCallbackInterface.java
  3. 20
      zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyCallbackInterface.java
  4. 5
      zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyThread.java
  5. 0
      zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketConfig.java
  6. 22
      zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketHelper.java
  7. 70
      zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TCPTask.java
  8. 83
      zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TcpServer.java
  9. 123
      zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java

56
zmt_module/src/main/java/io/dcloud/zmt_module/AsyncTask.java

@ -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();
}
}

12
zmt_module/src/main/java/io/dcloud/zmt_module/MyCallbackInterface.java

@ -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);
}
}

20
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);
}
}

5
zmt_module/src/main/java/io/dcloud/zmt_module/MyThread.java → zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyThread.java

@ -1,12 +1,15 @@
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")));
// server.initSocket((Integer.parseInt("16666")));
while (true){
String message = server.receiveMessage(SocketConfig.UTF_8);

0
zmt_module/src/main/java/io/dcloud/zmt_module/SocketConfig.java → zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketConfig.java

22
zmt_module/src/main/java/io/dcloud/zmt_module/SocketHelper.java → zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketHelper.java

@ -1,9 +1,14 @@
package io.dcloud.zmt_module;
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) {
@ -51,4 +56,19 @@ public class SocketHelper {
return sb.toString();
}
public static String getIPAddressForNetwork() throws SocketException {
String address = null;
for(Enumeration<NetworkInterface> enum1 = NetworkInterface.getNetworkInterfaces(); enum1.hasMoreElements();){
NetworkInterface networkInterface = enum1.nextElement();
for(Enumeration<InetAddress> 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;
}
}

70
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();
}
}

83
zmt_module/src/main/java/io/dcloud/zmt_module/TcpServer.java → zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TcpServer.java

@ -1,8 +1,6 @@
package io.dcloud.zmt_module;
package io.dcloud.zmt_module.tcpServer;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import com.alibaba.fastjson.JSONObject;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
@ -11,17 +9,15 @@ 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;
import io.dcloud.feature.uniapp.bridge.UniJSCallback;
import io.dcloud.zmt_module.SocketConfig;
import io.dcloud.zmt_module.tcpServer.SocketHelper;
public class TcpServer {
// 私有变量
@ -43,7 +39,7 @@ public class TcpServer {
* @param port 端口号0-65535
*
*/
public void initSocket(int port){
public void initSocket(int port, UniJSCallback uniJSCallback){
System.out.println(TAG+"initSocket");
try{
if (mServerSocket != null) { mServerSocket.close(); mServerSocket = null; }
@ -55,7 +51,42 @@ public class TcpServer {
// 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());
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();
}
}
}
@ -74,7 +105,16 @@ public class TcpServer {
// m_serverListenSocket.Listen(100);
// }
}catch (IOException e){
e.printStackTrace();
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();
}
}
}
@ -82,18 +122,8 @@ public class TcpServer {
void callback(Object object);
}
private String getIPAddressForNetwork() throws SocketException {
String address = null;
for(Enumeration<NetworkInterface> enum1 = NetworkInterface.getNetworkInterfaces(); enum1.hasMoreElements();){
NetworkInterface networkInterface = enum1.nextElement();
for(Enumeration<InetAddress> 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 Boolean isServerBound(){
return mServerSocket.isBound();
}
// 接收消息
@ -107,6 +137,9 @@ public class TcpServer {
// }
System.out.println(TAG+"tcp server receiveMessage ing...");
try{
if(mServerSocket==null){
return null;
}
System.out.println("mServerSocket:"+mServerSocket.toString());
// 获取输入信息
// if(mServerSocket==null){

123
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<String,Object> params=new HashMap<>();
//开启TCP,传入jsCallback
new TCPTask().task(port, callback);
// 全局回调
// Map<String,Object> 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)

Loading…
Cancel
Save