关于Websocket类,我应该何时应该调用onerror
,何时应该调用onclose
,何时应该调用它们?例如,如果我尝试连接到非现有服务器,我会得到一个onerror和一个onclose,如下所示:
Event {isTrusted: true, type: "error", target: WebSocket, currentTarget: WebSocket, eventPhase: 2…}
和
CloseEvent {wasClean: false, code: 1006, reason: "", type: "close", target: WebSocket…}
我正在使用redux,我希望按以下方式发送事件:
ws.onerror = (msg) =>{
dispatch(error(msg));
};
ws.onclose = (msg) => {
dispatch(close(msg));
};
但是我觉得我需要很好地理解可能触发的内容,以便我可以相应地设计我的redux状态。