ie9都不支援websocket,何況ie6,但是websocket能開發那麼酷的功能,怎麼能讓ie攔住我們的腳步?
但是怎麼辦?
用flash封裝吧
具體的flash程式碼如下:
package {
導入flash.display.Stage;
導入flash.display.Sprite;
導入flash.events.*;
導入flash.external.ExternalInterface 導入flash.system.Security;
導入flash.utils.Timer;
導入flash.net.Socket;
導入flash.utils.ByteArray;
導入flash.utils.Endian;
public class websocket4ie extends Sprite {
public static function main():void
{
var websocket4ie:websocket4ie = new websocket4ie(); >private var movieName:String;
私有變數處理程序:字串;
私人變數伺服器:字串;
私人變數連接埠:編號;
private var isDebug:Number;
私有var套接字:Socket;
private var socketBuffer:ByteArray = new ByteArray();
public function websocket4ie() {
Security.allowDomain("*");
var 計數器:數量0;
root.addEventListener(Event.ENTER_FRAME, function ():void { if ( counter > 100) counter = 0; });
this.movieName = root.loaderInfo.parameters.movif eName;
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);
嘗試{
this.debugEnabled = root.loaderInfo.parameters.debugEnabled = = "true" ?真:假;
} catch (例如:Object) {
this.debugEnabled = false;
}
this.connectServer();
ExternalInterface.addCallback(" sendData", this.sendData);
}
public function connectServer():void {
socket = new Socket();
socket.endian = EndEventian.BIG_ENDIANIAN socket.addEventListener(Event.CLOSE, onClose);
socket.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
socket.
socket.addEventListener(ProgressEvent.SOCKET_DATA, onSocketData);
socket.connect(this.server, this.port);
this.socket = 套接字;
}
publicunction onConnect(e:Event):void {
//握手
var headers:Array = new Array();
headers.push("GET /chat HTTP/1.1rn");
headers .push("升級:websocketrn");
headers.push("連線:Upgradern");
headers.push("主機:" this.server ":" this.port "rn");
headers.push("來源:nullrn");
headers.push("Sec-WebSocket-Key: 6z4ezNfATjW5/FEMYpqRuw==rn");
headers.push("Sec-WebSocket-版本: 13rnrnrn");
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 位置:Number = 0;
public function readOnData():void {
var tmpPos :Number = this.position;
this.socketBuffer.position = this.position;
//讀取一個0x81
if(this.socketBuffer.bytesAvailable>=1) {
var h: Number = this.socketBuffer.readUnsignedByte();
this.debug("頭:" h);
this.position = 1;
if(this.socketBuffer.bytesAvailable>=1) {
if(this.socketBuffer.bytesAvailable>=1) {
var len:Number = this.socketBuffer.readUnsignedByte();
this.debug("長度:" len);
this.position = 1;
if(lenif(this.socketBuffer.bytesAvailable>=len) {
this.onText(this.socketBuffer.readUTFBytes(len));
this.position = len;
this.readOnData();
this.position = len;
this.readOnData();
this.position = len;
this.readOnData();
this.position = len;
this.readOnData();
this.position = len;
this.readOnData();
this.position = len;
this.readOnData();
this.position >} else {
this.position = tmpPos;
返回;
}
} else if(len==126) {
if(this.socketBuffer.bytesAvailable>=2) {
if(this.socketBuffer.bytesAvailable>=2) {
if(this.socketBuffer.bytesAvailable>=2) {
var trueLen:Number = this.socketBuffer.readUnsignedShort();
if(this.socketBuffer.bytesAvailable>=trueLen) {
this.onText(this.socketBuffer.readUTFBytes(trueNen); >this.position = trueLen;
this.readOnData();
}
} else {
this.position = tmpPos;
回傳;
}
}
}
回傳;
}
}
}
回傳;
}
}
}
回傳;
}
} } 回傳; >} else { this.position = tmpPos; 回傳; } } else { this.position = tmpPos; 回傳; } } } public function onText(text:String):void { ExternalInterface.call(this.handlers ".onData",this.movieName,text); } public function writeBytes(text); } public function writeBytes(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);
休息;
} 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();
}
繼續;
}
}
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(字節);
}
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(lenhead.writeByte(len);
} else if(lenhead.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.movi物>ExternalInterface.call(this.handlers ".debug",this.movif eName, );
}
}
}
}
js程式碼如下
複製🎜>複製🎜>代碼
代碼如下:
var handlers = {
'connects':[],
'onClose':function(index,flagect) {
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);
}
};
函數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 = '
' ;
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(); }