Browse Source

添加closeTcp

module-tcpserver-mqtt-t4j-faceSDK
zmt 3 years ago
parent
commit
e861eaba93
  1. 2
      app/src/main/assets/apps/__UNI__2B9497D/www/app-service.js
  2. 2
      app/src/main/assets/apps/__UNI__2B9497D/www/app-view.js
  3. 2
      app/src/main/assets/apps/__UNI__2B9497D/www/pages/index/scan.js
  4. 2
      app/src/main/assets/apps/__UNI__2B9497D/www/pages/sample/ext-component.js
  5. 2
      app/src/main/assets/apps/__UNI__2B9497D/www/pages/sample/ext-module.js
  6. 20
      uniappWWW/unipluginDemo/pages/sample/ext-module-vue.vue
  7. 10
      zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TCPTask.java
  8. 58
      zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TcpServer.java
  9. 31
      zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.java

2
app/src/main/assets/apps/__UNI__2B9497D/www/app-service.js
File diff suppressed because it is too large
View File

2
app/src/main/assets/apps/__UNI__2B9497D/www/app-view.js
File diff suppressed because it is too large
View File

2
app/src/main/assets/apps/__UNI__2B9497D/www/pages/index/scan.js
File diff suppressed because it is too large
View File

2
app/src/main/assets/apps/__UNI__2B9497D/www/pages/sample/ext-component.js
File diff suppressed because it is too large
View File

2
app/src/main/assets/apps/__UNI__2B9497D/www/pages/sample/ext-module.js
File diff suppressed because it is too large
View File

20
uniappWWW/unipluginDemo/pages/sample/ext-module-vue.vue

@ -2,7 +2,8 @@
<div class="content">
<button class="btn" type="primary" @click="gotoNativePage">跳转原生Activity</button>
<button class="btn" type="primary" @click="tcpAsyncFunc">[开启TcpServer]</button>
<button class="btn" type="primary" @click="initTcp">[开启TcpServer]</button>
<button class="btn" type="primary" @click="closeTcp">[关闭TcpServer]</button>
<text class="tips">
点击后将开启TcpServer. 本机IP: 192.168.1.64 端口16666
</text>
@ -82,10 +83,10 @@
this.log(JSON.stringify(ret))
})
},
tcpAsyncFunc(){
initTcp(){
//
this.log("Click Tcp Server.")
zmtModule.tcpAsyncFunc({
zmtModule.initTcp({
'port':"16666",
},
(ret) => {
@ -96,6 +97,19 @@
this.log("invoke:"+JSON.stringify(ret))
})
},
closeTcp(){
//
this.log("Click Tcp Server.")
zmtModule.closeTcp({
},
(ret) => {
modal.toast({
message: ret,
duration: 2
});
this.log("invoke:"+JSON.stringify(ret))
})
},
testFun2(){
//
var ret = zmtModule.testSyncFunc({

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

@ -17,6 +17,7 @@ public class TCPTask {
TcpServer server = new TcpServer();
Thread myThread;
Boolean stopThread = false;
// TCP task
public void task(final Integer port, final UniJSCallback uniJSCallback){
// Android 4.0 之后不能在主线程中请求HTTP请求
@ -33,6 +34,10 @@ public class TCPTask {
server.initSocket(port,uniJSCallback);
while (true){
if(stopThread){
break;
}
String tcp_receive_message = server.receiveMessage(SocketConfig.UTF_8);//接收消息
if(tcp_receive_message!=null){
@ -67,4 +72,9 @@ public class TCPTask {
myThread.start();
}
public void closeTask(){
this.stopThread = true;
server.close();
}
}

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

@ -42,7 +42,7 @@ public class TcpServer {
public void initSocket(int port, UniJSCallback uniJSCallback){
System.out.println(TAG+"initSocket");
try{
if (mServerSocket != null) { mServerSocket.close(); mServerSocket = null; }
// if (mServerSocket != null) { mServerSocket.close(); mServerSocket = null; }
if (mServerSocket==null){
mServerSocket = new ServerSocket(port);
@ -59,34 +59,34 @@ public class TcpServer {
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();
}
}
// 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();
// }
//
// }
}

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

@ -13,11 +13,11 @@ public class zmtClass extends UniModule {
String TAG = "module plugin";
// final AbsSDKInstance uniSDK = this.mUniSDKInstance;//定义uniSDK
TCPTask myTask = new TCPTask();
//run ui thread
@UniJSMethod(uiThread = true)
public void tcpAsyncFunc(JSONObject options, final UniJSCallback callback) {
Log.e(TAG, "tcpAsyncFunc:"+options);
public void initTcp(JSONObject options, final UniJSCallback callback) {
Log.e(TAG, "tcpAsyncFunc:"+options+myTask.toString());
Integer port = 16666;//默认端口当uni传递port参数时采用传递port初始化tcp
String s_port = options.getString("port");
@ -28,7 +28,15 @@ public class zmtClass extends UniModule {
//开启TCP,传入jsCallback
new TCPTask().task(port, callback);
if(myTask!=null){
myTask.task(port, callback);
}else{
JSONObject data = new JSONObject();
data.put("code", "0");
data.put("toast", "请勿重复开启");
callback.invokeAndKeepAlive(data);
}
// 全局回调
// Map<String,Object> params=new HashMap<>();
@ -37,6 +45,21 @@ public class zmtClass extends UniModule {
}
//run JS thread
@UniJSMethod(uiThread = true)
public void closeTcp(JSONObject options, final UniJSCallback callback){
Log.e(TAG, "testSyncFunc--zmt2");
JSONObject data = new JSONObject();
data.put("code", "0");
data.put("toast", "closeTcp");
if(myTask!=null){
myTask.closeTask();
myTask = null;
}
callback.invokeAndKeepAlive(data);
}
@UniJSMethod(uiThread = true)
public void testAsyncFunc(JSONObject options, UniJSCallback callback) {
Log.e(TAG, "testAsyncFunc--zmt:"+options);

Loading…
Cancel
Save