上周在实现一个聊天的业务时,测试发现该功能在IE和Edge下是不能实现。在查看报错以后发现报错 Unhandled promise rejection ReferenceError: 'TextEncoder' is not defined
。 原来是stompJs使用了TextEncoder方法,但IE和Edge的JavaScript运行环境不支持该方法。 我找到了如下几种方法。 只针对SPA应用node
若是你的NodeJs版本升到了v11,那么node的运行环境是支持TextEncoder方法的。可是若是你的项目的nodeJs版本低于v11,能够安装moduletext-encodin
。web
$ npm install text-encoding
npm
加在入口文件promise
// These have been added in NodeJS v11, so good idea is to check first
if (typeof TextEncoder !== 'function') {
const TextEncodingPolyfill = require('text-encoding');
window.TextEncoder = TextEncodingPolyfill.TextEncoder;
window.TextDecoder = TextEncodingPolyfill.TextDecoder;
}
复制代码
由于第一种方法解决了个人方法,因此一下方法没有去实践。bash
据了解有两个备用库websocket
和ws
能够使用。websocket
$ npm install websocket
socket
在全局对象global
中添加ide
Object.assign(global, { WebSocket: require('websocket').w3cwebsocket });
测试
$ npm install ws
ui
Object.assign(global, { WebSocket: require('ws') });