当我尝试创建一个新的ReconnectingWebSocket时,我得到以下信息:
count
打字稿代码为:
reconnecting_websocket_1.default is not a constructor
在运行时,chrome开发工具中生成的javascript显示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
}
/* ... */
}
是预期的ReconnectingWebSocket类,但是reconnecting_websocket_1
未定义,我认为这是失败的根本原因。
default
NB:
this.reconnectingWebSocket = new reconnecting_websocket_1.default(this.url, [], wsOptions);
this.reconnectingWebSocket.onmessage = this.onMessage;
答案 0 :(得分:1)
将编译器选项esModuleInterop
添加到tsconfig.json。
您可能还想看看socket.io-client
,它稍大一些,但3年前没有被放弃。