我有一段通过UDP接收服务器广播然后写出服务器IP的JS,真机测试很正常,今天打包后的apk安装完就不正常,无法得到结果。
代码如下:
// search for server
function Str2Bytes(str){
var ch, st, re = [];
for (var i = 0; i < str.length; i++ ) {
ch = str.charCodeAt(i); // get char
st = []; // set up "stack"
do {
st.push( ch & 0xFF ); // push byte to stack
ch = ch >> 8; // shift value down by 1 byte
}
while ( ch );
// add stack contents to result
// done because chars have "wrong" endianness
re = re.concat( st.reverse() );
}
// return an array of bytes
return re;
}
function Bytes2Str(arr){
var str = "";
for(var i=0; i<arr.length; i++) {
var tmp = String.fromCharCode(arr[i]);
str += tmp;
}
return str;
}
function findServer(){
// 导入android.content.Intent类对象
var DatagramPacket = plus.android.importClass("java.net.DatagramPacket");
var DatagramSocket = plus.android.importClass("java.net.DatagramSocket");
var InetAddress = plus.android.importClass("java.net.InetAddress");
var SocketEx = plus.android.importClass("java.net.SocketException;");
// 导入后可以使用new方法创建类的示例对象
var ds = new DatagramSocket();
var messageReceived = false;
var serverAddress = "Server not found";
var data = [];
data = Str2Bytes("hello");
var port = 9989;
var iAdd = new InetAddress();
var dp = new DatagramPacket(data, data.length,iAdd.getByName("255.255.255.255"), port);
while (!messageReceived){
var d = [];
d = Str2Bytes("http://255.255.255.255:8898");
var dp2 = new DatagramPacket(d, d.length);
ds.send(dp);
ds.receive(dp2);
var dpData = Bytes2Str(dp2.getData());
var arrUrl = dpData.split("//");
serverAddress = arrUrl[1].substring(0,arrUrl[1].indexOf(":"));
return serverAddress;
messageReceived=true;
ds.close();
}
}
gadget2k (作者)
非常感谢
2015-12-14 20:13
barice
速度很快啊
2015-12-14 21:06
gadget2k (作者)
话说,打包机更新会通知大家吗?
2015-12-17 14:56
Android_磊子
回复 gadget2k:一般HBuilder更新的时候会写在releaseNote中,这些NJS问题都会归类到修复Native.js使用相关问题中
2015-12-17 15:42