错误或网络丢失时重新连接websocket redux-saga通道

时间:2020-03-12 08:54:26

标签: redux websocket redux-saga

以下示例和redux-saga文档中,我有一个为套接字创建redux-saga通道的功能,如下所示:

export function* initializeWebSocketChannel(param) {
  const mySocket = new SockJS(endpoints.WS);
  const channel = yield call(createEventChannel, mySocket, param);
  while (true) {
    const { unmountDashboard, response } = yield race({
      unmount: take(FETCH_STOP),
      response: take(channel),
    });
    if (unmount) {
      channel.close();
    } else {
      if (response.data) {
        yield put({
          type: FETCH_SUCCESS,
          payload: response.data,
        });
      }
      if (response.error) {
        yield put({
          type: FETCH_ERROR,
          payload: response.error,
        });
      }
    }
  }
}

问题是,当发生错误或没有Internet连接时,套接字将关闭并且永远不会再次启动。例如,如果每30秒发生这种情况,我该怎么办?我将尝试再次启动该频道?

0 个答案:

没有答案