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();
}
相关推荐
基于HTML5 WebSocket服务端的封装,绝对能用,让你不用费神捣鼓websocket方面的东西了,
flash websocket
vue 封装 webSocket调用 封装后的webSocket在组件中调用 压缩文件中包含socket.js和调用方法说明
这个代码例子使用了c c++实现了websocket 开发 包含了websocket服务器和websocket客户端,拥有详细的解释 这个库比libwebsocket更加简单方便,比libsocket更加高效便捷.
vue2.0 根据elementui写的WebSocket心跳和聊天重连封装
uniapp封装websocket,使用简单,下载复制进项目即可使用,统一使用json收发消息,区分业务类型。 1、使用方法,在需要使用的页面导入改文件(也可以挂载全局) 2、onLoad方法调用this.socketObj = new WebSocketIO...
websocket 连接简单封装
besthttp插件(实现了websocket)
利用C++实现了websocket server, 使用libuv作为实现tcp的基础,另外使用gbase作为工具
利用flash请求模仿websocket协议建立连接。使websocket兼容IE678,部署到tomcat7,运行index.jsp即可。完美运行websocket。
Qt基于QWebSocketServer,WebSocket的封装,和TCP/UDP一样,简单易用,直接引用头文件与资源文件,然后绑定几个信号与槽即可,并且跨平台测试通过。
这个websocket的demo实现了实时聊天,以及用flash socket兼容了IE8甚至IE5。demo都有代码注释,有问题的可以留言,我看到就会回复,详情请参考我另外一篇博文:http://www.cnblogs.com/againn/p/8308875.html
c#、.NET6实现websocket协议demo,完整清晰的解析和打包示例 包含请求头解析、数据帧解析、数据帧掩码处理、数据帧打包
thinkphp6使用workerman websocket连接
该资源用C语言实现的简易websocket测试程序,并附有web端的html页面辅助调试,供websocket学习者下载使用,理解websocket协议
由于websocket只支持IE10及以上版本,故本人专门整个支持IE10以下的demo供大家参考 java版本的demo,可以直接导入运行 需要布署在tomcat7及以上的服务器
websocket c语言实现, 包含httpparser, sha1 算法,wsparser,实现了websocket 服务器的协议部分。vs 作为ide,可修改到linux下实现
SuperWebSocket是WebSocket协议服务器端的.NET实现. 作为HTML5的一个重要新特性,WebSocket 规范的目标是在浏览器中实现和服务器端双向通信.双向通信可以拓展浏览器上的应用类型,例如实时的数据推送(股票行情),...