Here my Javascript code that is written in Angular2. The issue is important but more important is: how can I print this error on Html page because this page is going to be displayed in a mobile web-view, so that developers from the other side can get the error. Even if JSON.stringify() is not work.
let webSocketURL = "ws://localhost:7000";
message = '';
webSocket:any;
try {
this.webSocket = new WebSocket(webSocketURL);
this.webSocket.onopen = (openEvent)=>{
this.message = "WebSocket OPEN";
this.webSocket.send('hello from client');
};
this.webSocket.onclose = (closeEvent) =>{
console.log('closeEvent',closeEvent);
this.message = "WebSocket CLOSE"+ JSON.stringify(closeEvent);
alert("WebSocket CLOSE: " + JSON.stringify(closeEvent, null, 4));
};
} catch (exception) {
console.error(exception);
this.message = " Got exception" + exception ;
}
My HTML code is as below
<p>
This is stage 11 for socket demo and we are going to listion socket on ws://localhost:7000
</p>
<h3>Here is Message output ::{{message}} </h3>
My current output is as below:
What am I doing wrong?
If I get you correctly, you want to show the exception to other devs, if yes you can use this
let webSocketURL = "ws://localhost:7000";
message : any;
webSocket:any;
try {
this.message = null;
this.webSocket = new WebSocket(webSocketURL);
this.webSocket.onopen = (openEvent)=>{
this.message = "WebSocket OPEN";
this.webSocket.send('hello from client');
};
this.webSocket.onclose = (closeEvent) =>{
console.log('closeEvent',closeEvent);
this.message = "WebSocket CLOSE"+ JSON.stringify(closeEvent);
alert("WebSocket CLOSE: " + JSON.stringify(closeEvent, null, 4));
};
} catch (exception) {
console.error(exception);
this.message = exception ;
}
<p>
This is stage 11 for socket demo and we are going to listion socket on ws://localhost:7000
</p>
<h3 *ngIf="message">Here is Message output ::{{message | json}} </h3>
Pay attention I changed message type to any to have an object in there
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.