Browse Source

优化tcp回调

module-tcpserver-mqtt-t4j-faceSDK
zmt 3 years ago
parent
commit
95fbb39fe1
  1. 26
      zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TCPTask.java
  2. 15
      zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TcpServer.java
  3. 3
      zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java

26
zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TCPTask.java

@ -24,6 +24,7 @@ public class TCPTask {
public TcpServer server ; public TcpServer server ;
Thread myThread; Thread myThread;
Boolean stopThread = false; Boolean stopThread = false;
Integer messageCount = 0;
// TCP task // TCP task
public void sendTcpCallBack(String send_msg){ public void sendTcpCallBack(String send_msg){
@ -32,7 +33,6 @@ public class TCPTask {
} }
public void task(final Integer port, final UniJSCallback uniJSCallback){ public void task(final Integer port, final UniJSCallback uniJSCallback){
// Android 4.0 之后不能在主线程中请求HTTP请求
// System.out.println("AsyncTask thread running... ==>>>44444444:"+port+ server.isServerBound()); // System.out.println("AsyncTask thread running... ==>>>44444444:"+port+ server.isServerBound());
this.server = new TcpServer(); this.server = new TcpServer();
// this.server = server.initSocket(port,uniJSCallback); // this.server = server.initSocket(port,uniJSCallback);
@ -59,24 +59,30 @@ public class TCPTask {
// String send_msg = "5501a23070b6"; //回发socket消息确认接收 // String send_msg = "5501a23070b6"; //回发socket消息确认接收
// send_msg = SocketHelper.getMessageByReadLine(send_msg); // send_msg = SocketHelper.getMessageByReadLine(send_msg);
// server.sendMessage(send_msg, SocketConfig.UTF_8);//回发消息 // server.sendMessage(send_msg, SocketConfig.UTF_8);//回发消息
messageCount++;
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
data.put("code", "1");
data.put("count", messageCount);
data.put("threadID", Thread.currentThread().getId()); data.put("threadID", Thread.currentThread().getId());
data.put("threadName", Thread.currentThread().getName()); data.put("threadName", Thread.currentThread().getName());
// data.put("message", server.);
// data.put("message", tcp_receive_message);
data.put("message", tcp_receive_message); data.put("message", tcp_receive_message);
// myCallback.tcpCallback(data); // myCallback.tcpCallback(data);
// myTcpCallback.tcpCallback(data);//回调到zmtClass给uni端 // myTcpCallback.tcpCallback(data);//回调到zmtClass给uni端
uniJSCallback.invokeAndKeepAlive(data); uniJSCallback.invokeAndKeepAlive(data);
try {
Thread.sleep(100); //定义休眠时间
} catch (InterruptedException e) {
e.printStackTrace();
}
}else{
try {
Thread.sleep(1000); //定义休眠时间
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
try {
Thread.sleep(1000); //定义休眠时间
} catch (InterruptedException e) {
e.printStackTrace();
}
} }

15
zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TcpServer.java

@ -66,6 +66,7 @@ public class TcpServer {
inetAddress.getHostAddress()+" , address:"+inetAddress.getAddress().toString()+" 端口:"+port+ "----"+ SocketHelper.getIPAddressForNetwork()); inetAddress.getHostAddress()+" , address:"+inetAddress.getAddress().toString()+" 端口:"+port+ "----"+ SocketHelper.getIPAddressForNetwork());
com.alibaba.fastjson.JSONObject data = new com.alibaba.fastjson.JSONObject(); com.alibaba.fastjson.JSONObject data = new com.alibaba.fastjson.JSONObject();
data.put("code", "0");
data.put("port", port); data.put("port", port);
data.put("ip", SocketHelper.getIPAddressForNetwork()); data.put("ip", SocketHelper.getIPAddressForNetwork());
uniJSCallback.invokeAndKeepAlive(data); uniJSCallback.invokeAndKeepAlive(data);
@ -91,8 +92,8 @@ public class TcpServer {
if(e.getMessage().equals("bind failed: EADDRINUSE (Address already in use)")){ if(e.getMessage().equals("bind failed: EADDRINUSE (Address already in use)")){
System.out.println("已初始化过TCP,忽略该操作:"+e.getMessage()); System.out.println("已初始化过TCP,忽略该操作:"+e.getMessage());
JSONObject _data = new JSONObject(); JSONObject _data = new JSONObject();
_data.put("code",0);
_data.put("toast","已初始化过TCP,请勿重复执行初始化操作。");
_data.put("code","-1");
_data.put("toast","重复初始化");
uniJSCallback.invokeAndKeepAlive(_data); uniJSCallback.invokeAndKeepAlive(_data);
}else{ }else{
e.printStackTrace(); e.printStackTrace();
@ -208,15 +209,13 @@ public class TcpServer {
try { try {
mOutStream = mServer.getOutputStream(); mOutStream = mServer.getOutputStream();
mPrinter = new PrintWriter(new OutputStreamWriter(mOutStream,Charset.forName(charsetName))); mPrinter = new PrintWriter(new OutputStreamWriter(mOutStream,Charset.forName(charsetName)));
mOutStream.write(SocketHelper.hexToBytes(message));
mOutStream.flush();
// mPrinter.println(message); // mPrinter.println(message);
// mPrinter.flush(); // mPrinter.flush();
// mOutStream.write(message.getBytes(Charset.forName(charsetName))); // mOutStream.write(message.getBytes(Charset.forName(charsetName)));
mOutStream.write(SocketHelper.hexToBytes(message));
mOutStream.flush();
System.out.println(TAG+"TCP发送成功 :"+message); System.out.println(TAG+"TCP发送成功 :"+message);
} catch (IOException e) { } catch (IOException e) {
System.out.println(TAG+"tcp server sendMessage err :"+e); System.out.println(TAG+"tcp server sendMessage err :"+e);
e.printStackTrace(); e.printStackTrace();
@ -225,6 +224,8 @@ public class TcpServer {
}; };
mExecutorService.execute(mRunnable); mExecutorService.execute(mRunnable);
} }
public void sendMessageInServer(String message,String charsetName){ public void sendMessageInServer(String message,String charsetName){
final byte[] msg ; final byte[] msg ;
msg = message.getBytes(Charset.forName(charsetName)); msg = message.getBytes(Charset.forName(charsetName));

3
zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java

@ -33,7 +33,8 @@ public class zmtClass extends UniModule {
//开启TCP,传入jsCallback //开启TCP,传入jsCallback
// myTask = new TCPTask(); // myTask = new TCPTask();
myTCPTask.task(port, callback);
myTCPTask.task(port, callback); //code: -1 重复初始化 0 初始化成功 1 接收消息
// if(myTask!=null){ // if(myTask!=null){
// myTask.task(port, callback); // myTask.task(port, callback);
// }else{ // }else{

Loading…
Cancel
Save