zmt
3 years ago
13 changed files with 467 additions and 472 deletions
-
56zmt_module/src/main/java/io/dcloud/zmt_module/AsyncTask.java
-
12zmt_module/src/main/java/io/dcloud/zmt_module/MyCallbackInterface.java
-
20zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyCallbackInterface.java
-
5zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/MyThread.java
-
0zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketConfig.java
-
22zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/SocketHelper.java
-
70zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TCPTask.java
-
83zmt_module/src/main/java/io/dcloud/zmt_module/tcpServer/TcpServer.java
-
123zmt_module/src/main/java/io/dcloud/zmt_module/zmtClass.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(); |
|||
} |
|||
|
|||
} |
@ -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); |
|||
} |
|||
} |
@ -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); |
|||
} |
|||
} |
@ -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,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(); |
|||
} |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue