让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 相关文章推荐
Dojo之路:如何利用Dojo实现Drag and Drop效果
Apr 10 Javascript
IE event.srcElement和FF event.target 功能比较
Mar 01 Javascript
禁用键盘上的(全局)指定键兼容iE、Chrome、火狐
May 14 Javascript
JQuery中attr方法和removeAttr方法用法实例
May 18 Javascript
jquery实现点击页面回到顶部
Nov 23 Javascript
Vue学习笔记进阶篇之过渡状态详解
Jul 14 Javascript
详解从Vue.js源码看异步更新DOM策略及nextTick
Oct 11 Javascript
JavaScript实现修改伪类样式
Nov 27 Javascript
JavaScript设计模式之原型模式分析【ES5与ES6】
Jul 26 Javascript
JavaScript 格式化数字、金额、千分位、保留几位小数、舍入舍去
Jul 23 Javascript
详解基于原生JS验证表单组件xy-form
Aug 20 Javascript
jquery插件实现轮播图效果
Oct 19 jQuery
当自定义数据属性为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
使用网络地址转换实现多服务器负载均衡
2006/10/09 PHP
浅析php header 跳转
2013/06/17 PHP
zend optimizer在wamp的基础上安装图文教程
2013/10/26 PHP
PHP中你应该知道的require()文件包含的正确用法
2015/06/12 PHP
通过修改配置真正解决php文件上传大小限制问题(nginx+php)
2015/09/23 PHP
yii2 页面底部加载css和js的技巧
2016/04/21 PHP
php实现36进制与10进制转换功能示例
2017/01/10 PHP
PHP html_entity_decode()函数讲解
2019/02/25 PHP
解决laravel(5.5)访问public报错的问题
2019/10/12 PHP
JavaScript Cookie 直接浏览网站分网址
2009/12/08 Javascript
JQuery与JSon实现的无刷新分页代码
2011/09/13 Javascript
JS实现自动变换的菜单效果代码
2015/09/09 Javascript
使用Sticky组件实现带sticky效果的tab导航和滚动导航的方法
2016/03/22 Javascript
Javascript基础知识盲点总结之函数
2016/05/15 Javascript
JavaScript实现简单的拖动效果
2016/07/02 Javascript
详解Vue路由History mode模式中页面无法渲染的原因及解决
2017/09/28 Javascript
JavaScript面向对象编程小游戏---贪吃蛇代码实例
2019/05/15 Javascript
vue使用swiper实现左右滑动切换图片
2020/10/16 Javascript
[04:22]DOTA2上海特级锦标赛主赛事第四日TOP10
2016/03/06 DOTA
在Django的模型和公用函数中使用惰性翻译对象
2015/07/27 Python
python 构造三维全零数组的方法
2018/11/12 Python
python 顺时针打印矩阵的超简洁代码
2018/11/14 Python
Python面向对象进阶学习
2019/05/21 Python
Python 继承,重写,super()调用父类方法操作示例
2019/09/29 Python
html5跳转小程序wx-open-launch-weapp踩坑
2020/12/02 HTML / CSS
Luxplus荷兰:以会员价购买美容产品等,独家优惠
2019/08/30 全球购物
函授本科毕业生自我鉴定
2013/10/16 职场文书
文员岗位职责
2013/11/09 职场文书
中学生爱国演讲稿
2013/12/31 职场文书
资金主管岗位职责范本
2014/03/04 职场文书
主要领导对照检查材料
2014/08/26 职场文书
挂职学习心得体会
2014/09/09 职场文书
领导班子党的群众路线教育实践活动对照检查材料
2014/09/25 职场文书
机器人瓦力观后感
2015/06/12 职场文书
详解NodeJS模块化
2021/06/15 NodeJs
vue3 自定义图片放大器效果的示例代码
2022/07/23 Vue.js