当我尝试创建新的ReconnectingWebSocket时,得到以下信息:reconnecting_websocket_1.default is not a constructor
打字稿代码为:
import ReconnectingWebSocket, { Options } from 'reconnecting-websocket';
export class RealtimeClient {
private reconnectingWebSocket: ReconnectingWebSocket
constructor(private url: string, private dispatch: IDispatcher) {}
public connect() {
const wsOptions: Options = {
minReconnectionDelay: 1000,
reconnectionDelayGrowFactor: 1.0,
connectionTimeout: 2000,
}
this.reconnectingWebSocket = new ReconnectingWebSocket(this.url, [], wsOptions)
this.reconnectingWebSocket.onmessage = this.onMessage
}
/* ... */
}
在运行时,chrome开发人员工具中生成的javascript显示
reconnecting_websocket_1
是预期的ReconnectingWebSocket类,但是default
未定义,我认为这是失败的根本原因。this.reconnectingWebSocket = new reconnecting_websocket_1.default(this.url, [], wsOptions);
this.reconnectingWebSocket.onmessage = this.onMessage;
注意:
我正在编译到ES5 javascript(通过tsconfig.json目标)。
我对reconnecting-websocket具有非开发依赖
"reconnecting-websocket": "^4.0.0-rc5"
最佳答案
将编译器选项esModuleInterop
添加到tsconfig.json中。
您可能还想看看socket.io-client
,它稍大一些,但3年前没有被放弃。