我有一个使用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
变量。
答案 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';
}
}