代码之家  ›  专栏  ›  技术社区  ›  Syntax

Typescript重新连接WebSocket未能调用构造函数

  •  0
  • Syntax  · 技术社区  · 7 年前

    ReconnectingWebSocket 我得到以下信息:

    reconnecting_websocket_1.default is not a constructor

    typescript代码为:

    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 dev tools中生成的javascript显示 reconnecting_websocket_1 default 是未定义的,我认为这是失败的根本原因。

    this.reconnectingWebSocket = new reconnecting_websocket_1.default(this.url, [], wsOptions);
    this.reconnectingWebSocket.onmessage = this.onMessage;
    

    注意:

    1. 我正在编译到ES5JavaScript(通过tsconfig.json 目标
    2. 我对重新连接websocket没有开发依赖性 "reconnecting-websocket": "^4.0.0-rc5"
    1 回复  |  直到 7 年前
        1
  •  1
  •   Ebuall    7 年前

    添加编译器选项 esModuleInterop 到tsconfig.json。
    你可能还想看看 socket.io-client 稍大一点,但3年前没有废弃。

    推荐文章