测试下来,web-socket-js简直可以兼容到底了。因为web-socket-js是基于flash的,所以必须要求要求IE8, IE9必须是Flash Player 10 或者更新的版本。
web-socket-js是基于flash的技术,只需要引入两个js文件和一个swf文件,就可以让浏览器用于几乎原生的WebSocket接口。另外,web-socket-js还是需要在ws服务端843端口做一个flash安全策略文件的服务。
我自己曾经基于stompjs和web-socket-js,做WebSocket兼容到IE5 怪异模式, 当然了stompjs在低版本的IE上有兼容性问题, 而且stompjs已经不再维护了,你可以使用我fork的一个版本,地址是:https://github.com/wangduandu...
主要是老版本IE在正则表达式行为方面有点异常。
// fix ie8, ie9, RegExp not normal problem
// in chrome the frames length will be 2, but in ie8, ie9, it well be 1
// by wdd 20180321
if (frames.length === 1) {
frames.push('')
}
web-socket-js用法示例
websocket服务器的843端口还需要部署一个flash的安全策略文件服务
拷贝swfobject.js, web_socket.js, WebSocketMain.swf 到响应的目录下
编写代码如下
// Let the library know where WebSocketMain.swf is:
WEB_SOCKET_SWF_LOCATION = "WebSocketMain.swf";
// Write your code in the same way as for native WebSocket:
var ws = new WebSocket("ws://example.com:10081/");
ws.onopen = function() {
ws.send("Hello"); // Sends a message.
};
ws.onmessage = function(e) {
// Receives a message.
alert(e.data);
};
ws.onclose = function() {
alert("closed");
};