Redux-Saga套接字发送消息

时间:2020-03-28 15:42:58

标签: sockets websocket redux-saga

我正在尝试向套接字功能“ pong”发送消息,但是我有一个错误。
我从官方文档中复制了代码。

index.js:1错误:应用:类型[context,fn]的参数未定义或为空fn
检查时(io-6de156f3.js:33)
在validateFnDescriptor(io-6de156f3.js:467)上
适用时(io-6de156f3.js:537)
在pong(soket.action.js:29)
在pong.next()
在下一个(redux-saga-core.esm.js:1157)
在currCb(redux-saga-core.esm.js:1251)

import { eventChannel, END } from 'redux-saga';
import { take, put, call, fork, apply, delay } from 'redux-saga/effects';

function createWebSocketConnection() {
    return new Promise((resolve, reject) => {
        const socket = new WebSocket('ws://localhost:8080');
        socket.onopen = function() {
            resolve(socket);
        };
        socket.onerror = function(evt) {
            reject(evt);
        };
    });
}

function createSocketChannel(socket) {
    return eventChannel(emit => {
        socket.onmessage = event => {
            emit(event.data);
        };
        return () => {
            socket.close();
        };
    });
}

function* pong(socket) {
    yield delay(5000);
    yield apply(socket, socket.emit, ['pong']); // call `emit` as a method with `socket` as context
}

export function* saga() {
    const socket = yield call(createWebSocketConnection);
    const socketChannel = yield call(createSocketChannel, socket);
    try {
        while (true) {
            console.log('work')
            const message = yield take(socketChannel);

            console.log(message);
            yield fork(pong, socket);
        }
    } catch (e) {}
}

0 个答案:

没有答案