将来自 paho 客户端的数据保存在数组中,angular 4
Save data in array from paho client, angular 4
我想在名为 'single' 的数组中保存温度数据(我从 paho 客户端获取)。最后,我想在仪表中显示实时数据。
为此,我创建了一个服务来存储数据并将数据分发给多个组件。
但我只得到 ERR: "" is not assignable to parameter of type '{ value: string; }'。
我真的很感激对此有一些新的想法!谢谢!
我的服务:
import { Injectable } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import {Paho} from '../../../node_modules/ng2-mqtt/mqttws31';
@Injectable()
export class ClassdataproviderService {
public name: string;
public value: string;
single = [
{
name: 'eins',
value: '15'
},
{
name: 'zwei',
value: '20'
},
{
name: 'drei',
value: '73'
}
];
// Create a client instance
client: any;
packet: any;
constructor() {
this.client = new Paho.MQTT.Client('wpsdemo.gia.rwth-aachen.de', 8080, 'Steffen');
this.onMessage();
this.onConnectionLost();
// connect the client
this.client.connect({onSuccess: this.onConnected.bind(this)});
}
// called when the client connects
onConnected() {
console.log('Connected');
this.client.subscribe('node/m1/temperature');
//this.sendMessage('HelloWorld');
}
sendMessage(message: string) {
const packet = new Paho.MQTT.Message(message);
packet.destinationName = 'World';
this.client.send(packet);
}
// called when a message arrives
onMessage() {
this.client.onMessageArrived = (message: Paho.MQTT.Message) => {
console.log('Message arrived : ' + message.payloadString);
this.single.push('test', message.payloadString); **//<-- Here i want to push the data in my array**
};
}
// called when the client loses its connection
onConnectionLost() {
this.client.onConnectionLost = (responseObject: Object) => {
console.log('Connection lost : ' + JSON.stringify(responseObject));
};
}
我解决了问题。
您只需在 onMessageArrived 函数中使用此行将数据推送到数组(单个)中:
this.single.push({name: 'test', value: message.payloadString});
我想在名为 'single' 的数组中保存温度数据(我从 paho 客户端获取)。最后,我想在仪表中显示实时数据。
为此,我创建了一个服务来存储数据并将数据分发给多个组件。
但我只得到 ERR: "" is not assignable to parameter of type '{ value: string; }'。
我真的很感激对此有一些新的想法!谢谢!
我的服务:
import { Injectable } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import {Paho} from '../../../node_modules/ng2-mqtt/mqttws31';
@Injectable()
export class ClassdataproviderService {
public name: string;
public value: string;
single = [
{
name: 'eins',
value: '15'
},
{
name: 'zwei',
value: '20'
},
{
name: 'drei',
value: '73'
}
];
// Create a client instance
client: any;
packet: any;
constructor() {
this.client = new Paho.MQTT.Client('wpsdemo.gia.rwth-aachen.de', 8080, 'Steffen');
this.onMessage();
this.onConnectionLost();
// connect the client
this.client.connect({onSuccess: this.onConnected.bind(this)});
}
// called when the client connects
onConnected() {
console.log('Connected');
this.client.subscribe('node/m1/temperature');
//this.sendMessage('HelloWorld');
}
sendMessage(message: string) {
const packet = new Paho.MQTT.Message(message);
packet.destinationName = 'World';
this.client.send(packet);
}
// called when a message arrives
onMessage() {
this.client.onMessageArrived = (message: Paho.MQTT.Message) => {
console.log('Message arrived : ' + message.payloadString);
this.single.push('test', message.payloadString); **//<-- Here i want to push the data in my array**
};
}
// called when the client loses its connection
onConnectionLost() {
this.client.onConnectionLost = (responseObject: Object) => {
console.log('Connection lost : ' + JSON.stringify(responseObject));
};
}
我解决了问题。
您只需在 onMessageArrived 函数中使用此行将数据推送到数组(单个)中:
this.single.push({name: 'test', value: message.payloadString});