我能够通过以下方式解决这个问题
事件频道
上述示例的工作代码为:
export function* watchEditorAcceptInvitatioRequest(action) {
yield takeEvery(START_EDITOR_ACCEPT_INVITATION_SOCKET, edditorAcceptInvitationSocket)
}
export function* edditorAcceptInvitationSocket(action) {
const {token, user_id} = action.payload
const subscription = SocketIo.subscribe(token, '/broadcasting/authTest')
yield put(listenEditorAcceptInvitationSocket())
let channel = yield call(SocketIo.listen, subscription, `new-test-channel.${user_id}`,
'.new-test-test', receiveEditorAcceptInvitationSocket)
while (true) {
const action = yield take(channel)
yield put(action)
}
}
下面是更新的套接字订阅和监听代码:
从“..”导入商店/存储/配置存储'
从“laravel Echo”导入Echo
从“socket.io客户端”导入Socketo
从“redux saga”导入{eventChannel}
const BASE_URL = 'https://www.my domain.com'
const PORT = 1111
const subscribe = (token, authEndpoint) => new Echo({
broadcaster: 'socket.io',
host: `${BASE_URL}:${PORT}`,
client: Socketio,
authEndpoint: authEndpoint,
auth: {
headers: {
Authorization: 'Bearer ' + token,
Accept: 'application/json',
},
},
})
const listen = (echo, channel, event, callback) => {
return eventChannel( emitter => {
echo.private(channel).listen(event, e => {
emitter(store.dispatch(callback(e)))
})
return () => {
console.log('returning channel')
}
})}
export default {
subscribe,
listen
}