在版本3.2.2之后通过浏览器运行时,我无法实例化ReconnectingWebSocket。 在3.2.2和更低版本之前,一切正常。但是现在我遇到了以下错误:
client.ts:13 Uncaught TypeError: ReconnectingWebSocket is not a constructor
at Object.<anonymous> (client.ts:13)
at __webpack_require__ (bootstrap a23e24d7998736253177:19)
at Object.<anonymous> (main.ts:9)
at __webpack_require__ (bootstrap a23e24d7998736253177:19)
at bootstrap a23e24d7998736253177:62
at bootstrap a23e24d7998736253177:62
我的client.ts代码是:
import { listen } from 'vscode-ws-jsonrpc';
const ReconnectingWebSocket = require ('reconnecting-websocket');
const socketOptions = {
WebSocket: undefined,
maxReconnectionDelay: 10000,
minReconnectionDelay: 1000,
reconnectionDelayGrowFactor: 1.3,
connectionTimeout: 10000,
maxRetries: Infinity,
debug: false
};
const webSocket = new ReconnectingWebSocket('ws://localhost:3000/', undefined, socketOptions);
listen({
webSocket,
onConnection: connection => {
console.log('connected');
}
});
我检查了#95,但"esModuleInterop": true
对我不起作用。
这是我的tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"declarationMap": true,
"inlineSources": false,
"declaration": true,
"stripInternal": true,
"lib": [
"es6",
"dom"
],
"strict": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"rootDir": "src",
"outDir": "lib",
"baseUrl": ".",
"skipLibCheck": true,
"alwaysStrict": true,
"esModuleInterop": true
},
"include": [
"src"
]
}
还有一件事情,从终端node lib/client.js
运行相同的client.ts代码可以正常工作。但是,这同样会在浏览器中产生错误。在浏览器控制台typeof ReconnectingWebSocket
中进行调试时,表明它是一个对象,而不是一个函数。但是typeof ReconnectingWebSocket.default
作为函数给出。
因此,我将ReconnectingWebSocket与default
关键字一起使用
const webSocket = new ReconnectingWebSocket('ws://localhost:3000/', undefined, socketOptions).default;
现在在浏览器中可以正常工作。但是从终端运行时,同样会给出相同的错误。
请在3.2.2版本之后帮助使用ReconnectingWebSocket吗?