如何对回调函数采取行动

时间:2017-07-25 12:07:34

标签: angular socket.io

我有一个使用WebSocket连接到服务器的脚本

export class AppComponent {

  connection_status = false;
  message = '';

  public connect() {
    this.socket = io('http://localhost:5001');
    this.socket.on('connected', this.connection_established);
  }
}

我想在收到connection_status邮件时更改connected变量,并将邮件内容保存到message变量。

1 个答案:

答案 0 :(得分:1)

export class AppComponent {

  connection_status = false;
  message = '';

  public connect() {
    this.socket = io('http://localhost:5001');
    this.socket.on('connected', this.connection_established.bind(this));
  }

  connection_established() {
    this.connection_established = true;
    this.message = 'connected';
  }
}