如何处理意外事件,例如奇怪的数据,损坏的数据,意外的数据或事件监听器中出现的问题。
WebSocket
公开了onmessage
事件监听器。如果收到意想不到的东西,该如何处理?
通过抛出异常,登录到控制台,默默地忽略它?
console.log
是不是很糟糕?let socket = new WebSocket('wss://www.example.com/');
socket.onmessage = function (event) {
if (event.data === 'GARBAGE') {
// What is the appropriate thing to do here?
}
}
答案 0 :(得分:0)
It really depends on your application and how critical the problem is.
Ignoring an error should be a choice by design. If you are receiving an unexpected data to the listener, it means that your system is not working as intended somewhere. At least, you should know why you are getting such input.