立扬
立扬
  • 发布:2020-04-30 18:21
  • 更新:2020-05-06 17:05
  • 阅读:694

求助:app Socket作为服务端使用程序假死while的锅,如何解决,以下代码可以跑通,就是页面假死

分类:Native.js

if (plus.os.name == "Android") {
ServerSocket = plus.android.importClass("java.net.ServerSocket");
PrintWriter = plus.android.importClass("java.io.PrintWriter");
BufferedWriter = plus.android.importClass("java.io.BufferedWriter");
OutputStreamWriter = plus.android.importClass("java.io.OutputStreamWriter");
BufferedReader = plus.android.importClass("java.io.BufferedReader");
InputStreamReader = plus.android.importClass("java.io.InputStreamReader");
//var socket = new Socket("127.0.0.1", 8081);
//测试改良
var StrictMode = plus.android.importClass("android.os.StrictMode");
var Build = plus.android.importClass("android.os.Build");
if (Build.VERSION.SDK_INT > 9) {
var policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
var serverSocket = new ServerSocket(12345);

                socket = plus.android.importClass("java.net.Socket");  
                socket = serverSocket.accept();  

                //获取输出流  
                var outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());  
                var bufferWriter = new BufferedWriter(outputStreamWriter);  
                out = new PrintWriter(bufferWriter, true);  
                out.println("88888Conneted...");  

                //获取输入流  
                var inputStreamReader = new InputStreamReader(socket.getInputStream());  
                var br = new BufferedReader(inputStreamReader);  
                //console.log(msg);  
                var responseLine = "";  

                while ((responseLine = br.readLine()) != null) { //用readLine接收数据是,会自动抛弃换行符,如果为了保持数据的格式,需要在这里加上一个换行标识符  

                    //returnStr += responseLine + "\n";  
                    console.log(responseLine);  
                }  

参考网址如下:https://ask.dcloud.net.cn/question/60
https://blog.csdn.net/baidu_36583119/article/details/79697063

2020-04-30 18:21 负责人:无 分享
已邀请:
立扬

立扬 (作者)

没有遇到么?问题原因应该是br.readLine()是阻塞式接收。暂且用window.setInterval("getMsg()", 1000),代替了while 增加一行判断if (br.ready()) { msg = br.readLine();} 。问题解决,但这个貌似是个凑合的解决方式,求完美解决办法...

该问题目前已经被锁定, 无法添加新回复