Ie9 does not support websocket, not to mention ie6, but websocket can develop so cool functions, how can we let ie stop us?
But what should I do?
Use flash for encapsulation
The specific flash code is as follows::
Copy codeThe Code is 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> 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 {
// Handshake
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: 6z4eznfat00005/FEMYpqRuw ==\ r \ n ");
Headers. push ("Sec-WebSocket-Version: 13 \ 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 ("header:" + 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 );
}
}
}
}
The js Code is as follows:
Copy codeThe Code is 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 ();
}