帮我找到websocket兼容IE浏览器代码
时间: 2023-10-28 13:04:15 浏览: 100
web-socket-js-1.0.2.zip_flash js websocket_web scoket.js_websock
好的,以下是一个兼容 IE 浏览器的 WebSocket 实现代码:
```javascript
var socket = new WebSocket("ws://localhost:8080");
socket.onopen = function() {
console.log("WebSocket连接成功");
};
socket.onmessage = function(event) {
console.log("接收到消息:" + event.data);
};
socket.onclose = function(event) {
console.log("WebSocket连接关闭");
};
socket.onerror = function(event) {
console.log("WebSocket错误发生");
};
// IE 不支持 WebSocket 协议,需要使用替代方案
if (!window.WebSocket) {
window.WebSocket = window.MozWebSocket;
}
if (window.WebSocket) {
var socket = new WebSocket("ws://localhost:8080");
socket.onopen = function() {
console.log("WebSocket连接成功");
};
socket.onmessage = function(event) {
console.log("接收到消息:" + event.data);
};
socket.onclose = function(event) {
console.log("WebSocket连接关闭");
};
socket.onerror = function(event) {
console.log("WebSocket错误发生");
};
} else {
console.log("你的浏览器不支持WebSocket协议");
}
```
希望对你有用。
阅读全文