我有一个webSocket,我从服务器获取数据。我创建了一个返回数据的rx Subject MessageEvent。
但是,在我的observable上,我可以控制日志我的对象并打印json数据,但我无法访问任何属性,它返回undefined。
export interface Message {
request: string;
subscription: string,
}
@Injectable()
export class SocketProvider {
public message: Subject<Message>;
constructor(private socket: WebsocketProvider) {
console.log('Hello SocketProvider Provider');
this.message = <Subject<Message>>socket
.connect('ws://192.168.0.110:8081')
.map((response: MessageEvent): Message => {
let data = response.data;
return data;
})
}
}
和用法:
ionViewDidEnter() {
console.log('ionViewDidEnter ConfigsInputsPage');
this.subscribtion = this.socket.message.subscribe(msg => {
this.message = msg;
console.log("Response From webSocket server: " + msg);
console.log(msg.request);
});
this.socket.message.next(this.enterMessage);
}
private enterMessage = {
request: 'configs',
subscription: 'suscribe'
}
答案 0 :(得分:0)
好的。我添加以再次在socket.ts中解析json的事实:
constructor(private socket: WebsocketProvider) {
console.log('Hello SocketProvider Provider');
this.message = <Subject<Message>>socket
.connect('ws://192.168.0.110:8081')
.map((response: MessageEvent): Message => {
let data = JSON.parse(response.data); // <----------
return data;
})
}