让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实现的可以编辑选择的下拉框的代码
Nov 19 Javascript
JQuery.closest(),parent(),parents()寻找父结点
Feb 17 Javascript
jQuery中setTimeout的几种使用方法小结
Apr 07 Javascript
Jquery Post处理后不进入回调的原因及解决方法
Jul 15 Javascript
深入理解JavaScript系列(27):设计模式之建造者模式详解
Mar 03 Javascript
初识angular框架后的所思所想
Feb 19 Javascript
bootstrap组件之导航组件使用方法
Jan 19 Javascript
AngularJS实现路由实例
Feb 12 Javascript
深入理解react-router 路由的实现原理
Sep 26 Javascript
vue使用openlayers实现移动点动画
Sep 24 Javascript
jQuery实现手风琴特效
Jan 11 jQuery
JavaScript声明变量和数据类型的转换
Apr 12 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
PHP依赖倒置(Dependency Injection)代码实例
2014/10/11 PHP
php下Memcached入门实例解析
2015/01/05 PHP
盘点PHP和ASP.NET的10大对比!
2015/12/24 PHP
php微信公众号开发(2)百度BAE搭建和数据库使用
2016/12/15 PHP
对象的类型:本地对象(1)
2006/12/29 Javascript
Jquery iframe内部出滚动条
2010/02/11 Javascript
js+css在交互上的应用
2010/07/18 Javascript
jQuery事件 delegate()使用方法介绍
2012/10/30 Javascript
jQuery实现图片与文字描述左右滑动自动切换的方法
2015/07/27 Javascript
jQuery操作基本控件方法实例分析
2015/12/31 Javascript
jquery自动补齐功能插件flexselect用法示例
2016/08/06 Javascript
JS关闭窗口时产生的事件及用法示例
2016/08/20 Javascript
基于javascript实现的购物商城商品倒计时实例
2016/12/11 Javascript
微信小程序 HTTPS报错整理常见问题及解决方案
2016/12/14 Javascript
浅谈jQuery框架Ajax常用选项
2017/07/08 jQuery
jQuery实现简单日期格式化功能示例
2017/09/19 jQuery
解决iview打包时UglifyJs报错的问题
2018/03/07 Javascript
详解JavaScript的内存空间、赋值和深浅拷贝
2019/04/17 Javascript
重学JS之显示强制类型转换详解
2019/06/30 Javascript
jQuery提示框插件SweetAlert用法分析
2019/08/05 jQuery
NodeJs crypto加密制作token的实现代码
2019/11/15 NodeJs
[01:06]欢迎来到上海,TI9
2018/08/26 DOTA
python删除特定文件的方法
2015/07/30 Python
matplotlib 纵坐标轴显示数据值的实例
2018/05/25 Python
python实现根据文件关键字进行切分为多个文件的示例
2018/12/10 Python
Pyqt5 基本界面组件之inputDialog的使用
2019/06/25 Python
Python使用字典实现的简单记事本功能示例
2019/08/15 Python
使用matlab或python将txt文件转为excel表格
2019/11/01 Python
appium+python adb常用命令分享
2020/03/06 Python
python数据库操作mysql:pymysql、sqlalchemy常见用法详解
2020/03/30 Python
Python新建项目自动添加介绍和utf-8编码的方法
2020/12/26 Python
SQL语言面试题
2013/08/27 面试题
《母鸡》教学反思
2014/02/25 职场文书
个人总结与自我评价2015
2015/03/11 职场文书
MySQL 时间类型的选择
2021/06/05 MySQL
Java 获取Word中所有的插入和删除修订的方法
2022/04/06 Java/Android