Angular 每个接收消息的角度mqtt更新值

Angular 每个接收消息的角度mqtt更新值,angular,mqtt,Angular,Mqtt,我正在将mqtt协议与websocket一起使用,因此我使用浏览器来显示接收值。 问题是(在我的示例中)当我执行函数subscribeNewTopic()时,我可以手动console.log接收消息,但如果我收到另一条消息,因为我手动运行函数,所以不会收到新消息。我希望有一个函数,每当函数接收到新值时,console.log都会记录结果。。。。可能吗 这是我的角函数: subscribeNewTopic(): void { console.log('inside subscribe

我正在将
mqtt协议
websocket
一起使用,因此我使用浏览器来显示接收值。 问题是(在我的示例中)当我执行函数
subscribeNewTopic()
时,我可以手动
console.log
接收消息,但如果我收到另一条消息,因为我手动运行函数,所以不会收到新消息。我希望有一个函数,每当函数接收到新值时,console.log都会记录结果。。。。可能吗

这是我的角函数:

  subscribeNewTopic(): void {
    console.log('inside subscribe new topic')
    this.subscription = this._mqttService.observe(this.topicname).subscribe((message: IMqttMessage) => {
      this.msg = message;
      console.log('msg: ', message)
      console.log(message.payload.toString())
      this.logMsg('Message: ' + message.payload.toString() + '<br> for topic: ' + message.topic);
    });
    this.logMsg('subscribed to topic: ' + this.topicname)
  }

  logMsg(message): void {
    this.msglog.nativeElement.innerHTML += '<br><hr>' + message;
  }
subscribeNewTopic():void{
console.log('内部订阅新主题')
this.subscription=this.\u mqttService.observe(this.topicname).subscripte((消息:IMqttMessage)=>{
this.msg=消息;
console.log('msg:',message)
console.log(message.payload.toString())
this.logMsg('Message:'+Message.payload.toString()+'
用于主题:'+Message.topic); }); this.logMsg('订阅主题:'+this.topicname) } logMsg(消息):无效{ this.msglog.nativeElement.innerHTML+='

'+消息; }
我认为,您的代码应该可以正常工作。在代码的这一部分中:

this.subscription = this._mqttService.observe(this.topicname).subscribe((message: IMqttMessage) => {
      this.msg = message;
      console.log('msg: ', message)
      console.log(message.payload.toString())
      this.logMsg('Message: ' + message.payload.toString() + '<br> for topic: ' + message.topic);
});
this.subscription=this.\u mqttService.observe(this.topicname).subscription((消息:IMqttMessage)=>{
this.msg=消息;
console.log('msg:',message)
console.log(message.payload.toString())
this.logMsg('Message:'+Message.payload.toString()+'
用于主题:'+Message.topic); });
this.msg
应该具有您主题的最新值

如果您需要更多帮助,也许您可以尝试更精确地解释“因为我手动运行该函数”的含义。
也试着读给这个公会听,这会很有帮助