|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区
您需要 登录 才可以下载或查看,没有账号?立即注册
×
Android 4.4 基于Chromium内核的WebSocket实现
IamOkay 前天
在Android版本4.4之前,由于维护和开发Android版本时使用的是AppleWebkit开源内核,虽然也非常不错,但不支持许多html5 api,在Android4.4使用了Chromium才得以发展,
目前支持的html 5如下:
Web Workers 支持 javaScript多线程
WebSocket 支持 javascript套接字,TCP长链接
IDBFactory/indexDB 支持 索引数据库
ApplicationCache 支持 web离线缓存
postMessage/onMessage 支持 收发消息
ondeviceorientation,ondevicemotion,onorientationchange 支持 屏幕旋转,移动
onvolumechange 支持 声音改变
RequestAnimationFrame 支持 页面UI动画更新引擎
LocalStorage/sessionStorage
支持 本地缓存
FileReader 支持 本地文件读取
FormData 支持 模拟表单,表单模型
EventSource 支持 Server-Sent Events(SSE)功能,允许服务端推送数据到客户端。(通常叫数据推送)
CacheStorage 不支持 异步缓存
Promise 不支持 异步范式
Crypto 不支持 javascript加密API
WebAudio 不支持 流媒体播放
WebRTC 不支持 流媒体通讯
WebGL 不支持 Web GL图像框架
GeoLocation 支持 地理定位
在这里我们主要了解WebSocket在Android WebView上的支持,这里给出一个基于php WebSocketServer的例子:
【以下例子来自开源中国博客】
体验位置:http://www.yxsss.com/ui/sk.html
(请使用你的PC浏览器和你的Android4.4的设备上的浏览器)
php端
001
<?php
002
error_reporting(E_ALL ^ E_NOTICE);
003
ob_implicit_flush();
004
005
$sk=new Sock('127.0.0.1',8000);
006
$sk->run();
007
class Sock{
008
public $sockets;
009
public $users;
010
public $master;
011
012
private $sda=array();//已接收的数据
013
private $slen=array();//数据总长度
014
private $sjen=array();//接收数据的长度
015
private $ar=array();//加密key
016
private $n=array();
017
018
public function __construct($address, $port){
019
$this->master=$this->WebSocket($address, $port);
020
$this->sockets=array($this->master);
021
}
022
023
024
function run(){
025
while(true){
026
$changes=$this->sockets;
027
$write=NULL;
028
$except=NULL;
029
socket_select($changes,$write,$except,NULL);
030
foreach($changes as $sock){
031
if($sock==$this->master){
032
$client=socket_accept($this->master);
033
$key=uniqid();
034
$this->sockets[]=$client;
035
$this->users[$key]=array(
036
'socket'=>$client,
037
'shou'=>false
038
);
039
}else{
040
$len=0;
041
$buffer='';
042
do{
043
$l=socket_recv($sock,$buf,1000,0);
044
$len+=$l;
045
$buffer.=$buf;
046
}while($l==1000);
047
$k=$this->search($sock);
048
if($len<7){
049
$this->send2($k);
050
continue;
051
}
052
if(!$this->users[$k]['shou']){
053
$this->woshou($k,$buffer);
054
}else{
055
$buffer = $this->uncode($buffer,$k);
056
if($buffer==false){
057
continue;
058
}
059
$this->send($k,$buffer);
060
}
061
}
062
}
063
064
}
065
066
}
067
068
function close($k){
069
socket_close($this->users[$k]['socket']);
070
unset($this->users[$k]);
071
$this->sockets=array($this->master);
072
foreach($this->users as $v){
073
$this->sockets[]=$v['socket'];
074
}
075
$this->e("keyk close");
076
}
077
078
function search($sock){
079
foreach ($this->users as $k=>$v){
080
if($sock==$v['socket'])
081
return $k;
082
}
083
return false;
084
}
085
086
function WebSocket($address,$port){
087
$server = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
088
socket_set_option($server, SOL_SOCKET, SO_REUSEADDR, 1);
089
socket_bind($server, $address, $port);
090
socket_listen($server);
091
$this->e('Server Started : '.date('Y-m-d H:i:s'));
092
$this->e('Listening on : '.$address.' port '.$port);
093
return $server;
094
}
095
096
097
function woshou($k,$buffer){
098
$buf = substr($buffer,strpos($buffer,'Sec-WebSocket-Key:')+18);
099
$key = trim(substr($buf,0,strpos($buf,"\r\n")));
100
101
$new_key = base64_encode(sha1($key."258EAFA5-E914-47DA-95CA-C5AB0DC85B11",true));
102
103
$new_message = "HTTP/1.1 101 Switching Protocols\r\n";
104
$new_message .= "Upgrade: websocket\r\n";
105
$new_message .= "Sec-WebSocket-Version: 13\r\n";
106
$new_message .= "Connection: Upgrade\r\n";
107
$new_message .= "Sec-WebSocket-Accept: " . $new_key . "\r\n\r\n";
108
109
socket_write($this->users[$k]['socket'],$new_message,strlen($new_message));
110
$this->users[$k]['shou']=true;
111
return true;
112
113
}
114
115
function uncode($str,$key){
116
$mask = array();
117
$data = '';
118
$msg = unpack('H*',$str);
119
$head = substr($msg[1],0,2);
120
if ($head == '81' && !isset($this->slen[$key])) {
121
$len=substr($msg[1],2,2);
122
$len=hexdec($len);
123
if(substr($msg[1],2,2)=='fe'){
124
$len=substr($msg[1],4,4);
125
$len=hexdec($len);
126
$msg[1]=substr($msg[1],4);
127
}else if(substr($msg[1],2,2)=='ff'){
128
$len=substr($msg[1],4,16);
129
$len=hexdec($len);
130
$msg[1]=substr($msg[1],16);
131
}
132
$mask[] = hexdec(substr($msg[1],4,2));
133
$mask[] = hexdec(substr($msg[1],6,2));
134
$mask[] = hexdec(substr($msg[1],8,2));
135
$mask[] = hexdec(substr($msg[1],10,2));
136
$s = 12;
137
$n=0;
138
}else if($this->slen[$key] > 0){
139
$len=$this->slen[$key];
140
$mask=$this->ar[$key];
141
$n=$this->n[$key];
142
$s = 0;
143
}
144
145
$e = strlen($msg[1])-2;
146
for ($i=$s; $i<= $e; $i+= 2) {
147
$data .= chr($mask[$n%4]^hexdec(substr($msg[1],$i,2)));
148
$n++;
149
}
150
$dlen=strlen($data);
151
152
if($len > 255 && $len > $dlen+intval($this->sjen[$key])){
153
$this->ar[$key]=$mask;
154
$this->slen[$key]=$len;
155
$this->sjen[$key]=$dlen+intval($this->sjen[$key]);
156
$this->sda[$key]=$this->sda[$key].$data;
157
$this->n[$key]=$n;
158
return false;
159
}else{
160
unset($this->ar[$key],$this->slen[$key],$this->sjen[$key],$this->n[$key]);
161
$data=$this->sda[$key].$data;
162
unset($this->sda[$key]);
163
return $data;
164
}
165
166
}
167
168
169
function code($msg){
170
$frame = array();
171
$frame[0] = '81';
172
$len = strlen($msg);
173
if($len < 126){
174
$frame[1] = $len<16?'0'.dechex($len):dechex($len);
175
}else if($len < 65025){
176
$s=dechex($len);
177
$frame[1]='7e'.str_repeat('0',4-strlen($s)).$s;
178
}else{
179
$s=dechex($len);
180
$frame[1]='7f'.str_repeat('0',16-strlen($s)).$s;
181
}
182
$frame[2] = $this->ord_hex($msg);
183
$data = implode('',$frame);
184
return pack("H*", $data);
185
}
186
187
function ord_hex($data) {
188
$msg = '';
189
$l = strlen($data);
190
for ($i= 0; $i<$l; $i++) {
191
$msg .= dechex(ord($data{$i}));
192
}
193
return $msg;
194
}
195
196
//用户加入
197
function send($k,$msg){
198
parse_str($msg,$g);
199
$ar=array();
200
if($g['type']=='add'){
201
$this->users[$k]['name']=$g['ming'];
202
$ar['type']='add';
203
$ar['name']=$g['ming'];
204
$key='all';
205
}else{
206
$ar['nrong']=$g['nr'];
207
$key=$g['key'];
208
}
209
$this->send1($k,$ar,$key);
210
}
211
212
function getusers(){
213
$ar=array();
214
foreach($this->users as $k=>$v){
215
$ar[]=array('code'=>$k,'name'=>$v['name']);
216
}
217
return $ar;
218
}
219
220
//$k 发信息人的code $key接受人的 code
221
function send1($k,$ar,$key='all'){
222
$ar['code1']=$key;
223
$ar['code']=$k;
224
$ar['time']=date('m-d H:i:s');
225
$str = $this->code(json_encode($ar));
226
if($key=='all'){
227
$users=$this->users;
228
if($ar['type']=='add'){
229
$ar['type']='madd';
230
$ar['users']=$this->getusers();
231
$str1 = $this->code(json_encode($ar));
232
socket_write($users[$k]['socket'],$str1,strlen($str1));
233
unset($users[$k]);
234
}
235
foreach($users as $v){
236
socket_write($v['socket'],$str,strlen($str));
237
}
238
}else{
239
socket_write($this->users[$k]['socket'],$str,strlen($str));
240
socket_write($this->users[$key]['socket'],$str,strlen($str));
241
}
242
}
243
244
//用户退出
245
function send2($k){
246
$this->close($k);
247
$ar['type']='rmove';
248
$ar['nrong']=$k;
249
$this->send1(false,$ar,'all');
250
}
251
252
function e($str){
253
//$path=dirname(__FILE__).'/log.txt';
254
$str=$str."\n";
255
//error_log($str,3,$path);
256
echo iconv('utf-8','gbk//IGNORE',$str);
257
}
258
}
259
?>
client端
001
<!doctype html>
002
<html>
003
<head>
004
<meta charset="utf-8">
005
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no"/>
006
<title>HTML5 websocket 网页聊天室 javascript php</title>
007
<style type="text/css">
008
body,p{margin:0px; padding:0px; font-size:14px; color:#333; font-family:Arial, Helvetica, sans-serif;}
009
#ltian,.rin{width:98%; margin:5px auto;}
010
#ltian{border:1px #ccc solid;overflow-y:auto; overflow-x:hidden; position:relative;}
011
#ct{margin-right:111px; height:100%;overflow-y:auto;overflow-x: hidden;}
012
#us{width:110px; overflow-y:auto; overflow-x:hidden; float:right; border-left:1px #ccc solid; height:100%; background-color:#F1F1F1;}
013
#us p{padding:3px 5px; color:#08C; line-height:20px; height:20px; cursor:pointer; overflow:hidden; white-space:nowrap; text-overflow:ellipsis;}
014
#us p:hover,#us p:active,#us p.ck{background-color:#069; color:#FFF;}
015
#us p.my:hover,#us p.my:active,#us p.my{color:#333;background-color:transparent;}
016
button{float:right; width:80px; height:35px; font-size:18px;}
017
input{width:100%; height:30px; padding:2px; line-height:20px; outline:none; border:solid 1px #CCC;}
018
.rin p{margin-right:160px;}
019
.rin span{float:right; padding:6px 5px 0px 5px; position:relative;}
020
.rin span img{margin:0px 3px; cursor:pointer;}
021
.rin span form{position:absolute; width:25px; height:25px; overflow:hidden; opacity:0; top:5px; right:5px;}
022
.rin span input{width:180px; height:25px; margin-left:-160px; cursor:pointer}
023
024
#ct p{padding:5px; line-height:20px;}
025
#ct a{color:#069; cursor:pointer;}
026
#ct span{color:#999; margin-right:10px;}
027
.c2{color:#999;}
028
.c3{background-color:#DBE9EC; padding:5px;}
029
.qp{position:absolute; font-size:12px; color:#666; top:5px; right:130px; text-decoration:none; color:#069;}
030
#ems{position:absolute; z-index:5; display:none; top:0px; left:0px; max-width:230px; background-color:#F1F1F1; border:solid 1px #CCC; padding:5px;}
031
#ems img{width:44px; height:44px; border:solid 1px #FFF; cursor:pointer;}
032
#ems img:hover,#ems img:active{border-color:#A4B7E3;}
033
#ems a{color:#069; border-radius:2px; display:inline-block; margin:2px 5px; padding:1px 8px; text-decoration:none; background-color:#D5DFFD;}
034
#ems a:hover,#ems a:active,#ems a.ck{color:#FFF; background-color:#069;}
035
.tc{text-align:center; margin-top:5px;}
036
</style>
037
</head>
038
039
<body>
040
<div id="ltian">
041
<div id="us" class="jb"></div>
042
<div id="ct"></div>
043
<a href="javascript:;" class="qp" onClick="this.parentNode.children[1].innerHTML=''">清屏</a>
044
</div>
045
<div class="rin">
046
<button id="sd">发送</button>
047
<span><img src="http://www.yxsss.com/ui/sk/t.png" title="表情" id="imgbq"><img src="http://www.yxsss.com/ui/sk/e.png" title="上传图片"><form><input type="file" title="上传图片" id="upimg"></form></span>
048
<p><input id="nrong"></p>
049
</div>
050
<div id="ems"><p></p><p class="tc"></p></div>
051
<script>
052
if(typeof(WebSocket)=='undefined'){
053
alert('你的浏览器不支持 WebSocket ,推荐使用Google Chrome 或者 Mozilla Firefox');
054
}
055
</script>
056
<script src="http://www.yxsss.com/ui/p/a.js" type="text/javascript"></script>
057
<script>
058
(function(){
059
var key='all',mkey;
060
var users={};
061
var url='ws://127.0.0.1:8000';
062
var so=false,n=false;
063
var lus=A.$('us'),lct=A.$('ct');
064
function st(){
065
n=prompt('请给自己取一个响亮的名字:');
066
n=n.substr(0,16);
067
if(!n){
068
return ;
069
}
070
so=new WebSocket(url);
071
so.onopen=function(){
072
if(so.readyState==1){
073
so.send('type=add&ming='+n);
074
}
075
}
076
077
so.onclose=function(){
078
so=false;
079
lct.appendChild(A.$$('<p class="c2">退出聊天室</p>'));
080
}
081
082
so.onmessage=function(msg){
083
eval('var da='+msg.data);
084
var obj=false,c=false;
085
if(da.type=='add'){
086
var obj=A.$$('<p>'+da.name+'</p>');
087
lus.appendChild(obj);
088
cuser(obj,da.code);
089
obj=A.$$('<p><span>['+da.time+']</span>欢迎<a>'+da.name+'</a>加入</p>');
090
c=da.code;
091
}else if(da.type=='madd'){
092
mkey=da.code;
093
da.users.unshift({'code':'all','name':'大家'});
094
for(var i=0;i<da.users.length;i++){
095
var obj=A.$$('<p>'+da.users.name+'</p>');
096
lus.appendChild(obj);
097
if(mkey!=da.users.code){
098
cuser(obj,da.users.code);
099
}else{
100
obj.className='my';
101
document.title=da.users.name;
102
}
103
}
104
obj=A.$$('<p><span>['+da.time+']</span>欢迎'+da.name+'加入</p>');
105
users.all.className='ck';
106
}
107
108
if(obj==false){
109
if(da.type=='rmove'){
110
var obj=A.$$('<p class="c2"><span>['+da.time+']</span>'+users[da.nrong].innerHTML+'退出聊天室</p>');
111
lct.appendChild(obj);
112
users[da.nrong].del();
113
delete users[da.nrong];
114
}else{
115
da.nrong=da.nrong.replace(/{\\(\d+)}/g,function(a,b){
116
return '<img src="sk/'+b+'.gif">';
117
}).replace(/^data\:image\/png;base64\,.{50,}$/i,function(a){
118
return '<img src="'+a+'">';
119
});
120
//da.code 发信息人的code
121
if(da.code1==mkey){
122
obj=A.$$('<p class="c3"><span>['+da.time+']</span><a>'+users[da.code].innerHTML+'</a>对我说:'+da.nrong+'</p>');
123
c=da.code;
124
}else if(da.code==mkey){
125
if(da.code1!='all')
126
obj=A.$$('<p class="c3"><span>['+da.time+']</span>我对<a>'+users[da.code1].innerHTML+'</a>说:'+da.nrong+'</p>');
127
else
128
obj=A.$$('<p><span>['+da.time+']</span>我对<a>'+users[da.code1].innerHTML+'</a>说:'+da.nrong+'</p>');
129
c=da.code1;
130
}else if(da.code==false){
131
obj=A.$$('<p><span>['+da.time+']</span>'+da.nrong+'</p>');
132
}else if(da.code1){
133
obj=A.$$('<p><span>['+da.time+']</span><a>'+users[da.code].innerHTML+'</a>对'+users[da.code1].innerHTML+'说:'+da.nrong+'</p>');
134
c=da.code;
135
}
136
}
137
}
138
if(c){
139
obj.children[1].onclick=function(){
140
users[c].onclick();
141
}
142
}
143
lct.appendChild(obj);
144
lct.scrollTop=Math.max(0,lct.scrollHeight-lct.offsetHeight);
145
}
146
}
147
A.$('sd').onclick=function(){
148
if(!so){
149
return st();
150
}
151
var da=A.$('nrong').value.trim();
152
if(da==''){
153
alert('内容不能为空');
154
return false;
155
}
156
A.$('nrong').value='';
157
so.send('nr='+esc(da)+'&key='+key);
158
}
159
A.$('nrong').onkeydown=function(e){
160
var e=e||event;
161
if(e.keyCode==13){
162
A.$('sd').onclick();
163
}
164
}
165
function esc(da){
166
da=da.replace(/</g,'<').replace(/>/g,'>').replace(/\"/g,'"');
167
return encodeURIComponent(da);
168
}
169
function cuser(t,code){
170
users[code]=t;
171
t.onclick=function(){
172
t.parentNode.children.rcss('ck','');
173
t.rcss('','ck');
174
key=code;
175
}
176
}
177
A.$('ltian').style.height=(document.documentElement.clientHeight - 70)+'px';
178
st();
179
180
181
var bq=A.$('imgbq'),ems=A.$('ems');
182
var l=80,r=4,c=5,s=0,p=Math.ceil(l/(r*c));
183
var pt='sk/';
184
bq.onclick=function(e){
185
var e=e||event;
186
if(!so){
187
return st();
188
}
189
ems.style.display='block';
190
document.onclick=function(){
191
gb();
192
}
193
ct();
194
try{e.stopPropagation();}catch(o){}
195
}
196
197
for(var i=0;i<p;i++){
198
var a=A.$$('<a href="javascript:;">'+(i+1)+'</a>');
199
ems.children[1].appendChild(a);
200
ef(a,i);
201
}
202
ems.children[1].children[0].className='ck';
203
204
function ct(){
205
var wz=bq.weiz();
206
with(ems.style){
207
top=wz.y-242+'px';
208
left=wz.x+bq.offsetWidth-235+'px';
209
}
210
}
211
212
function ef(t,i){
213
t.onclick=function(e){
214
var e=e||event;
215
s=i*r*c;
216
ems.children[0].innerHTML='';
217
hh();
218
this.parentNode.children.rcss('ck','');
219
this.rcss('','ck');
220
try{e.stopPropagation();}catch(o){}
221
}
222
}
223
224
function hh(){
225
var z=Math.min(l,s+r*c);
226
for(var i=s;i<z;i++){
227
var a=A.$$('<img src="'+pt+i+'.gif">');
228
hh1(a,i);
229
ems.children[0].appendChild(a);
230
}
231
ct();
232
}
233
234
function hh1(t,i){
235
t.onclick=function(e){
236
var e=e||event;
237
A.$('nrong').value+='{\\'+i+'}';
238
if(!e.ctrlKey){
239
gb();
240
}
241
try{e.stopPropagation();}catch(o){}
242
}
243
}
244
245
function gb(){
246
ems.style.display='';
247
A.$('nrong').focus();
248
document.onclick='';
249
}
250
hh();
251
A.on(window,'resize',function(){
252
A.$('ltian').style.height=(document.documentElement.clientHeight - 70)+'px';
253
ct();
254
})
255
256
var fimg=A.$('upimg');
257
var img=new Image();
258
var dw=400,dh=300;
259
A.on(fimg,'change',function(ev){
260
if(!so){
261
st();
262
return false;
263
}
264
if(key=='all'){
265
alert('由于资源限制 发图只能私聊');
266
return false;
267
}
268
var f=ev.target.files[0];
269
if(f.type.match('image.*')){
270
var r = new FileReader();
271
r.onload = function(e){
272
img.setAttribute('src',e.target.result);
273
};
274
r.readAsDataURL(f);
275
}
276
});
277
img.onload=function(){
278
ih=img.height,iw=img.width;
279
if(iw/ih > dw/dh && iw > dw){
280
ih=ih/iw*dw;
281
iw=dw;
282
}else if(ih > dh){
283
iw=iw/ih*dh;
284
ih=dh;
285
}
286
var rc = A.$$('canvas');
287
var ct = rc.getContext('2d');
288
rc.width=iw;
289
rc.height=ih;
290
ct.drawImage(img,0,0,iw,ih);
291
var da=rc.toDataURL();
292
so.send('nr='+esc(da)+'&key='+key);
293
}
294
295
})();
296
</script>
297
</body>
298
</html> |
|