我正在订阅 2 个聊天室频道。该代码将停留在C第二个通道收到新消息之前。然后一直停留在A第一个通道收到新消息为止。
我怎样才能让2个通道分开工作。
function* ChatRoomPageEnter() {
const chanChat = yield call($stomp.eventChannel, 'ChatRoomPage', '/topic/chat');
const chanEcho = yield call($stomp.eventChannel, 'ChatRoomPage', '/user/queue/echo');
while (true) { // eslint-disable-line no-constant-condition
console.log('A');
const messageChat = yield take(chanChat);
console.log('B');
yield put({ type: 'chatroom/newMessage', payload: messageChat });
console.log('C'); // <----
const messageEcho = yield take(chanEcho);
console.log('D');
yield put({ type: 'chatroom/newMessage', payload: messageEcho });
console.log('E');
}
}
Run Code Online (Sandbox Code Playgroud)
为什么不创建一个观察者作为单独的函数并调用它两次。例如:
function * chatWatcher(chanName) {
while(true) {
const message = yield take(chanName);
yield put({ type: 'chatroom/newMessage', payload: message });
}
}
function* ChatRoomPageEnter() {
const chanChat = yield call($stomp.eventChannel, 'ChatRoomPage', '/topic/chat');
const chanEcho = yield call($stomp.eventChannel, 'ChatRoomPage', '/user/queue/echo');
yield * chatWatcher(chanChat);
yield * chatWatcher(chanEcho);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1573 次 |
| 最近记录: |