让ie6也支持websocket采用flash封装实现


Posted in Javascript onFebruary 18, 2013

ie9都不支持websocket,何况ie6,但是websocket能开发那么酷的功能,怎么能让ie拦住我们的脚步?
但是怎么办?
用flash封装吧

具体的flash代码如下:

package { 
import flash.display.Stage; 
import flash.display.Sprite; 
import flash.events.*; 
import flash.external.ExternalInterface; 
import flash.system.Security; 
import flash.utils.Timer; 
import flash.net.Socket; 
import flash.utils.ByteArray; 
import flash.utils.Endian; 
public class websocket4ie extends Sprite { 
public static function main():void 
{ 
var websocket4ie:websocket4ie = new websocket4ie(); 
} 
private var debugEnabled:Boolean; 
private var movieName:String; 
private var handlers:String; 
private var server:String; 
private var port:Number; 
private var isDebug:Number; 
private var socket:Socket; 
private var socketBuffer:ByteArray = new ByteArray(); 
public function websocket4ie() { 
Security.allowDomain("*"); 
var counter:Number = 0; 
root.addEventListener(Event.ENTER_FRAME, function ():void { if (++counter > 100) counter = 0; }); 
this.movieName = root.loaderInfo.parameters.movieName; 
this.handlers = root.loaderInfo.parameters.handlers; 
this.server = root.loaderInfo.parameters.server; 
this.port = root.loaderInfo.parameters.port; 
this.isDebug = root.loaderInfo.parameters.debug; 
this.debug(this.port+''+this.server); 
try { 
this.debugEnabled = root.loaderInfo.parameters.debugEnabled == "true" ? true : false; 
} catch (ex:Object) { 
this.debugEnabled = false; 
} 
this.connectServer(); 
ExternalInterface.addCallback("sendData", this.sendData); 
} 
public function connectServer():void { 
socket = new Socket(); 
socket.endian = Endian.BIG_ENDIAN; 
socket.addEventListener(Event.CONNECT, onConnect); 
socket.addEventListener(Event.CLOSE, onClose); 
socket.addEventListener(IOErrorEvent.IO_ERROR, onIOError); 
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError); 
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData); 
socket.connect(this.server, this.port); 
this.socket = socket; 
} 
public function onConnect(e:Event):void { 
//握手 
var headers:Array = new Array(); 
headers.push("GET /chat HTTP/1.1\r\n"); 
headers.push("Upgrade: websocket\r\n"); 
headers.push("Connection: Upgrade\r\n"); 
headers.push("Host: "+this.server+":"+this.port+"\r\n"); 
headers.push("Origin: null\r\n"); 
headers.push("Sec-WebSocket-Key: 6z4ezNfATjW5/FEMYpqRuw==\r\n"); 
headers.push("Sec-WebSocket-Version: 13\r\n\r\n\r\n"); 
this.socket.writeUTFBytes(headers.join('')); 
this.socket.flush(); 
} 
public function onTrueConnect():void { 
ExternalInterface.call(this.handlers+".onConnect",this.movieName); 
} 
public function onClose(e:Event):void { 
ExternalInterface.call(this.handlers+".onClose",this.movieName,'1'); 
} 
public function onIOError(e:IOErrorEvent):void { 
ExternalInterface.call(this.handlers+".onClose",this.movieName,'2'); 
} 
public function onSecurityError(e:SecurityErrorEvent):void { 
ExternalInterface.call(this.handlers+".onClose",this.movieName,'3'); 
} 
public var step:String = "head"; 
public var position:Number = 0; 
public function readOnData():void { 
var tmpPos:Number = this.position; 
this.socketBuffer.position = this.position; 
//read 一个 0x81 
if(this.socketBuffer.bytesAvailable>=1) { 
var h:Number = this.socketBuffer.readUnsignedByte(); 
this.debug("头:"+h); 
this.position += 1; 
if(this.socketBuffer.bytesAvailable>=1) { 
var len:Number = this.socketBuffer.readUnsignedByte(); 
this.debug("长度:"+len); 
this.position += 1; 
if(len<=125) { 
if(this.socketBuffer.bytesAvailable>=len) { 
this.onText(this.socketBuffer.readUTFBytes(len)); 
this.position += len; 
this.readOnData(); 
} else { 
this.position = tmpPos; 
return; 
} 
} else if(len==126) { 
if(this.socketBuffer.bytesAvailable>=2) { 
var trueLen:Number = this.socketBuffer.readUnsignedShort(); 
if(this.socketBuffer.bytesAvailable>=trueLen) { 
this.onText(this.socketBuffer.readUTFBytes(trueLen)); 
this.position += trueLen; 
this.readOnData(); 
} 
} else { 
this.position = tmpPos; 
return; 
} 
} 
} else { 
this.position = tmpPos; 
return; 
} 
} else { 
this.position = tmpPos; 
return; 
} 
} 
public function onText(text:String):void { 
ExternalInterface.call(this.handlers+".onData",this.movieName,text); 
} 
public function writeBytes(bytes:ByteArray):void { 
this.socketBuffer.position = this.socketBuffer.length; 
this.socketBuffer.writeBytes(bytes,0,bytes.length); 
this.debug("buffer数据:"+this.socketBuffer.length); 
this.readOnData(); 
} 
public var is_head:Boolean = true; 
public var header:ByteArray = new ByteArray(); 
public var headers:Array = new Array(); 
public function onSocketData(e:Event):void { 
var bytes:ByteArray = new ByteArray(); 
if(this.is_head) { 
while(this.socket.bytesAvailable) { 
var x:Number = this.socket.readUnsignedByte(); 
if(x==0x81) { 
this.is_head = false; 
bytes.writeByte(0x81); 
this.debug(this.headers); 
break; 
} else { 
this.header.writeByte(x); 
if(x==10) { 
this.header.position = 0; 
this.headers.push(this.header.readUTFBytes(this.header.length)); 
if(this.header.length==2) { 
this.onTrueConnect(); 
} 
this.header = new ByteArray(); 
} 
continue; 
} 
} 
if(this.socket.bytesAvailable) { 
this.socket.readBytes(bytes,1,this.socket.bytesAvailable); 
} 
} else { 
this.socket.readBytes(bytes,0,this.socket.bytesAvailable); 
} 
bytes.position = 0; 
this.writeBytes(bytes); 
} 
public function sendData(text:String):void { 
var head:ByteArray = new ByteArray(); 
head.writeByte(0x81); 
var body:ByteArray = new ByteArray(); 
body.writeUTFBytes(text); 
var len:Number = body.length; 
if(len<=125) { 
head.writeByte(len); 
} else if(len<65536){ 
head.writeByte(126); 
head.writeShort(len); 
} else { 
head.writeByte(127); 
head.writeUnsignedInt(len); 
} 
body.position = 0; 
head.position = 0; 
this.socket.writeBytes(head); 
this.socket.writeBytes(body); 
this.socket.flush(); 
} 
public function debug(str:*):void { 
if(this.isDebug) { 
ExternalInterface.call(this.handlers+".debug",this.movieName,str); 
} 
} 
} 
}

js代码如下
var handlers = { 
'connects':[], 
'onClose':function(index,flag) { 
this.connects[index.replace("socket_","")].onClose(); 
}, 
'onConnect':function(index) { 
this.connects[index.replace("socket_","")].onConnect(); 
}, 
'onData':function(index,text) { 
this.connects[index.replace("socket_","")].onData(text); 
}, 
'debug':function(index,str) { 
console.log(str); 
} 
}; 
function socket4ie() { 
this.debug = 0; 
this.init = function() { 
this.index = handlers.connects.length; 
handlers.connects.push(this); 
} 
this.connect = function(domain,port) { 
this.createFlash(domain,port); 
} 
this.createFlash = function(domain,port) { 
var html = '<object id="socket_'+this.index+'" type="application/x-shockwave-flash" data="websocket4ie.swf" width=0 height=0 class="swfupload">\ 
<param name="wmode" value="window">\ 
<param name="movie" value="websocket4ie.swf">\ 
<param name="quality" value="high">\ 
<param name="menu" value="false">\ 
<param name="allowScriptAccess" value="always">\ 
<param name="flashvars" value="movieName=socket_'+this.index+'&handlers=handlers&server='+domain+'&port='+port+'&debug='+this.debug+'"></object>'; 
var div = document.createElement('div'); 
div.id = "flash_"+this.index; 
div.innerHTML = html; 
document.body.appendChild(div); 
} 
this.onClose = function() { 
} 
this.onConnect = function() { 
} 
this.onData = function(text) { 
} 
this.init(); 
}
Javascript 相关文章推荐
jquery 触发a链接点击事件解决方案
May 02 Javascript
JS 操作Array数组的方法及属性实例解析
Jan 08 Javascript
JavaScript实现的Tween算法及缓冲特效实例代码
Nov 03 Javascript
JS作为值的函数用法示例
Jun 20 Javascript
Node.js开发第三方微信公众平台
Jun 05 Javascript
详解angularjs的数组传参方式的简单实现
Jul 28 Javascript
微信小程序实现分享到朋友圈功能
Jul 19 Javascript
浅谈Redux中间件的实践
Jul 27 Javascript
vue实现记事本功能
Jun 26 Javascript
localstorage实现带过期时间的缓存功能
Jun 28 Javascript
Node.js创建一个Express服务的方法详解
Jan 06 Javascript
js实现盒子滚动动画效果
Aug 09 Javascript
当自定义数据属性为json格式字符串时jQuery的data api问题探讨
Feb 18 #Javascript
jQuery筛选器children()案例详解(图文)
Feb 17 #Javascript
正则表达式搭配js轻松处理json文本方便而老古
Feb 17 #Javascript
jQuery图片播放8款精美插件分享
Feb 17 #Javascript
jQuery性能优化28条建议你值得借鉴
Feb 16 #Javascript
JavaScript调用堆栈及setTimeout使用方法深入剖析
Feb 16 #Javascript
js nextSibling属性和previousSibling属性概述及使用注意
Feb 16 #Javascript
You might like
采用header定义为文件然后readfile下载(隐藏下载地址)
2014/01/31 PHP
详解Grunt插件之LiveReload实现页面自动刷新(两种方案)
2015/07/31 PHP
PHP使用Memcache时模拟命名空间及缓存失效问题的解决
2016/02/27 PHP
Yii中的relations数据关联查询及统计功能用法详解
2016/07/14 PHP
PDO::commit讲解
2019/01/27 PHP
Code:loadScript( )加载js的功能函数
2007/02/02 Javascript
学习YUI.Ext第七日-View&amp;JSONView Part Two-一个画室网站的案例
2007/03/10 Javascript
EasyUI 中 MenuButton 的使用方法
2012/07/14 Javascript
深入理解jQuery中live与bind方法的区别
2013/12/18 Javascript
js插件方式打开pdf文件(浏览器pdf插件分享)
2013/12/20 Javascript
angularJS中router的使用指南
2015/02/09 Javascript
JS根据生日算年龄的方法
2015/05/05 Javascript
javascript拖拽应用实例
2016/03/25 Javascript
js实现页面a向页面b传参的方法
2016/05/29 Javascript
javascript数字验证的实例代码(推荐)
2016/08/20 Javascript
jquery checkbox的相关操作总结
2016/10/17 Javascript
JavaScript常用正则验证函数实例小结【年龄,数字,Email,手机,URL,日期等】
2017/01/23 Javascript
基于Vue.js与WordPress Rest API构建单页应用详解
2019/09/16 Javascript
Node.js API详解之 querystring用法实例分析
2020/04/29 Javascript
使用Python开发windows GUI程序入门实例
2014/10/23 Python
简单掌握Python中glob模块查找文件路径的用法
2016/07/05 Python
Python3读取Excel数据存入MySQL的方法
2018/05/04 Python
Tesserocr库的正确安装方式
2018/10/19 Python
python 发送和接收ActiveMQ消息的实例
2019/01/30 Python
Python基于mysql实现学生管理系统
2019/02/21 Python
python实现抖音点赞功能
2019/04/07 Python
Python利用PyExecJS库执行JS函数的案例分析
2019/12/18 Python
CSS3 真的会替代 SCSS 吗
2021/03/09 HTML / CSS
澳大利亚婴儿礼品公司:The Baby Gift Company
2018/11/04 全球购物
澳大利亚音乐商店:Bava’s Music City
2019/05/05 全球购物
汽车运用工程专业毕业生推荐信
2013/12/25 职场文书
租赁意向书范本
2014/04/01 职场文书
基层党支部公开承诺书
2014/05/29 职场文书
医院领导班子查摆问题对照检查材料思想汇报
2014/10/08 职场文书
党员干部反四风民主生活会对照检查材料思想汇报
2014/10/12 职场文书
2016医师资格考试考生诚信考试承诺书
2016/03/25 职场文书