javascript - 发生错误时尝试连接到 redux-saga 中的套接字
问题描述
我使用以下渠道准备了 saga:
function* createEventChannel(socket, dataItem) {
const stompClient = Stomp.over(socket);
const token = yield select(makeSelectToken);
return eventChannel(emit => {
stompClient.connect(
getAuthorization(token),
frame => {
stompClient.subscribe('/queue/data', message => {
const response = JSON.parse(message.body);
emit(response);
});
stompClient.subscribe('/queue/error', message => {
const error = JSON.parse(message.body);
emit(error);
});
stompClient.send(
'/register',
{},
JSON.stringify({
request: {
type: dataItem,
},
})
);
},
error => emit({ error: 'Lost connection' })
);
return () => {
socket.close();
console.log('return close: ');
};
});
}
export function* initializeChannel(
actionNames,
requestParam
) {
const mySocket = new SockJS('/ws');
const channel = yield call(createEventChannel, mySocket, requestParam);
while (true) {
const { unmount, response } = yield race({
unmount: take(UNMNOUT),
response: take(channel),
});
if (unmount) {
channel.close();
} else {
if (response) {
yield put({
type: FETCH_SUCCESS,
payload: response.data,
});
}
if (response.error) {
yield put({
type: actionNames.FETCH_ERROR,
payload: response.error,
});
}
}
}
}
我知道这是一段相当长的代码,但这就是我设法准备的。现在我面临三个问题:
- 当发生错误时,例如“连接丢失”,我想尝试重新连接 saga,例如每 30 秒。如何做到这一点?
- 在
stompClient.send
我将 dataItem 注册到队列的部分中。现在每次我再次创建套接字。我怎样才能做到这一点,我可以一个接一个地注册 dataItem ,只加入现有的队列和套接字? - 当套接字在线时,它不会对互联网丢失做出反应,我搜索了许多关于它的已打开问题,但我仍然不知道 - 有没有办法在 websocket 工作期间检查连接?
解决方案
推荐阅读
- python - 使用 osmnx 从多边形中检索数据
- flutter - Flutter 显示数据并在点击时更改它
- javascript - 根据字符串数组修改对象值
- python - 在 python 中安装兼容库
- android - Android Studio:等待本机模块的类型“握手”错误
- discord.js - 我需要一个间隔脚本或其他可以帮助我解决我的目标的东西
- css - 带有 Angular Material 的 Angular 8.3 不适用于 Mozilla 34 和 45
- c++ - C++ Imgui 绘制了我的 SDL opengl 窗口
- applescript - sass --watch 不检查文件是否为空,仅检查文件是否存在?
- ssl - vsFTP - gnutls_record_recv 中的 GnuTLS 错误 -15:收到了意外的 TLS 数据包