Typescript的观察者模式和成员变量生命周期

时间:2017-02-27 06:23:15

标签: javascript node.js typescript

在这里,我做了一个简单的例子,使用WebSocket来理解打字稿和&amp ;;的模式(如oberserver)。应该怎么写。 示例代码包括WebSocket的包装,在最后一行代码中,有可执行代码。 当我使用node命令运行它时,如下所示,

#tsc test.ts 
#node test.js
        this.m_websocket.send("Hello World!");
                        ^
TypeError: Cannot read property 'send' of undefined
    at WebSocketClient.onConnected (/Users/tehokang/Documents/Eclipse/jee/hucast.receiver/out/receiver/utils/websocket_client.js:17:25)
    at WebSocket.onOpen (/Users/tehokang/Documents/Eclipse/jee/hucast.receiver/node_modules/ws/lib/EventTarget.js:120:16)
    at emitNone (events.js:86:13)
    at WebSocket.emit (events.js:186:7)
    at WebSocket.setSocket (/Users/tehokang/Documents/Eclipse/jee/hucast.receiver/node_modules/ws/lib/WebSocket.js:167:10)
    at ClientRequest._req.on (/Users/tehokang/Documents/Eclipse/jee/hucast.receiver/node_modules/ws/lib/WebSocket.js:708:10)
    at emitThree (events.js:116:13)
    at ClientRequest.emit (events.js:195:7)
    at Socket.socketOnData (_http_client.js:443:11)
    at emitOne (events.js:96:13)

我希望会员(m_websocket)可以拨打电话'发送'在WebSocketClient的onConnected中,WebSocket甚至成员都是未定义的。我无法理解为什么会这样。我应该怀疑解决这个问题?

以下是我尝试的完整示例代码。

var WebSocket = require('ws');
export interface IWebSocketClientListener {
    onConnected(url:string) : void;
    onDisconnected(url:string) : void;
    onReceived(url:string, message:any) : void;
    onError(url:string, error:any) : void;
}

export class WebSocketClient {
    private m_server_url : string;
    private m_websocket : WebSocket;
    private m_listener : IWebSocketClientListener;

    constructor(listener : IWebSocketClientListener) {
        this.m_listener = listener;
    }

    public connect(server_url : string ) : void {
        this.m_server_url = server_url;
        this.m_websocket = new WebSocket(server_url);
        this.m_websocket.onopen = this.onConnected;
        this.m_websocket.onclose = this.onDisconnected;
        this.m_websocket.onmessage = this.onReceived;
        this.m_websocket.onerror = this.onError;
    }

    public onConnected() : void {
        this.m_websocket.send("Hello World!");
    }

    public onDisconnected() : void {
        this.m_listener.onDisconnected(this.m_server_url);
    }

    public onReceived(message:any) : void {
        this.m_listener.onReceived(this.m_server_url, message);
    }

    public onError(error:any) : void {
        this.m_listener.onError(this.m_server_url, error);
    }

    public send(message:any) : void {
        this.m_websocket.send(message);
    }
};

/**
 * @note Below lines are example of websocket client.
 */
var websocket_client : WebSocketClient = new WebSocketClient({
    onConnected(url:string) : void {
        console.log('Connected to ' + url);
    },
    onDisconnected(url:string) : void {
        console.log('Disconnected from ' + url);
    },
    onReceived(url:string, message:any) : void {
        console.log('Received from ' + url);
        console.log(message);
    },
    onError(url:string, error:any) : void {
        console.log(error.data);
    }
} as IWebSocketClientListener);

websocket_client.connect("ws://192.168.0.30:4434");

当我用以下内容替换回调时,我可以看到我的预期运作良好。

public onConnected = () => {
    this.m_listener.onConnected(this.m_server_url);
}

public onDisconnected = () => {
    this.m_listener.onDisconnected(this.m_server_url);
}

public onReceived = (message:any) => {
    this.m_listener.onReceived(this.m_server_url, message);
}

public onError = (error:any) => {
    this.m_listener.onError(this.m_server_url, error);
}

0 个答案:

没有答案