IE9 don't support websocket, not to mention IE6, but websocket can develop so cool function, how can let ie stop our footsteps?
But what?
Use Flash package.
the specific flash code is as follows:
Copy Code code as follows:
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 >) 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 {
Shake hands
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 a 0x81
if (this.socketbuffer.bytesavailable>=1) {
var h:number = This.socketBuffer.readUnsignedByte ();
This.debug ("head:" +h);
This.position + 1;
if (this.socketbuffer.bytesavailable>=1) {
var len:number = This.socketBuffer.readUnsignedByte ();
This.debug ("Length:" +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 data:" +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 code is as follows
Copy Code code as follows:
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 ();
}