先看了几篇文章,开发大概和web端相同,但是部分机型不兼容(Android4.4以下)。
[其它机型(ios和Android4.4以上)都是直接和web端相同使用]
1. http://ask.dcloud.net.cn/article/1103
试了一下,,貌似不行 0.0,估计使用姿势错误,而且该方面知识不足,于是改用其它方法
2. http://ask.dcloud.net.cn/question/60
把评论里Chuck和Sychel两位的代码合一起 使用,,如下代码块。
连接是成功了,,但是不知道如果监听使用 如 onopen、onmessage、onerror、onclose,,
搞了几个钟,时间不等人,,,下次再研究(这个估计是能实现,,知识不够0.0);
3. http://ask.dcloud.net.cn/question/1114
后端不是使用node.js,并且 貌似会发送polling请求;
4. https://github.com/anismiles/websocket-android-phonegap
尝试使用websocket-android-phonegap
mui.plusReady(function () {
testSocket()
})
function testSocket(){
if (plus.os.name == "Android") {
var Socket = plus.android.importClass("java.net.Socket");
var PrintWriter = plus.android.importClass("java.io.PrintWriter");
var BufferedWriter = plus.android.importClass("java.io.BufferedWriter");
var OutputStreamWriter = plus.android.importClass("java.io.OutputStreamWriter");
var BufferedReader = plus.android.importClass("java.io.BufferedReader");
var InputStreamReader = plus.android.importClass("java.io.InputStreamReader");
//测试改良
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 socket = new Socket("10.0.2.2", 8282);
var outputStreamWriter = new OutputStreamWriter(socket.getOutputStream());
var bufferWriter = new BufferedWriter(outputStreamWriter);
var out = new PrintWriter(bufferWriter, true);
out.println("GET / HTTP/1.1\r\n\
Host: zhang.qzone.com:8282\r\n\
Connection: Upgrade\r\n\
Pragma: no-cache\r\n\
Cache-Control: no-cache\r\n\
Upgrade: websocket\r\n\
Origin: http://zhang.qzone.com\r\n\
Sec-WebSocket-Version: 13\r\n\
Sec-WebSocket-Key: WvKJ6q11EWhzDwiIVBa31w==\r\n\
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n\r\n");
var inputStreamReader = new InputStreamReader(socket.getInputStream());
var br = new BufferedReader(inputStreamReader);
var msg = br.readLine();
//while(true)
{
if(msg != null)
{
console.log(msg);
}
//msg = br.readLine();
}
}
//console.log(mac);
alert("Done");
}
1 个评论
要回复文章请先登录或注册
huangr_001