简体   繁体   English

反应 redux 传奇 websocket 币安 stream

[英]react redux saga websocket binance stream

Not able to dispatch the action from saga, please have a look in the foo saga, here is the complete code and recent push github link May I know where I am going wrong?无法从 saga 发送动作,请查看 foo saga,这是完整代码和最近推送的github 链接我可以知道我哪里出错了吗? without saga it is working fine, but i want to implement trade app with saga.没有 saga 它工作正常,但我想用 saga 实现贸易应用程序。

import { takeEvery, put, call, take } from 'redux-saga/effects'
import {
    updateMarketPairs
} from '../actions/updateMarketPairs'

export default function* marketPairsSagas() {
    yield takeEvery("UPDATE_MARKET_PAIRS", connectSocketStreams)

}

function doAction (data){
    let ticker = {}
    data.forEach(item => {
        let symbol = item.symbol || item.s;
        ticker[symbol] = {
            symbol: symbol,
            lastPrice: item.lastPrice || item.c,
            priceChange: item.priceChange || item.p,
            priceChangePercent: item.priceChangePercent || item.P,
            highPrice: item.highPrice || item.h,
            lowPrice: item.lowPrice || item.l,
            quoteVolume: item.quoteVolume || item.q,
        }
    }) 
    return ticker;

}

function* foo (tickerdata) {
    console.log("yielding");  // appears in console
    console.log('after yielding tickerdata',tickerdata);
    yield put(updateMarketPairs(tickerdata));  // action *is not* dispatched
    console.log("yielded"); //appears in console
}

 function connectSocketStreams(action) {
    const payload = action.payload
    const allStreams = payload.streams && payload.streams.join('/');
    let connection = btoa(allStreams);
    connection = new WebSocket(`wss://stream.binance.com:9443/stream?streams=${allStreams}`);
    connection.onmessage = evt => {
        // yield put({ type: 'ACTION_TYPE', payload: JSON.parse(evt.data)})
        let tickerdata = doAction(JSON.parse(evt.data).data);
        const iter = foo(tickerdata);
        iter.next();
        // yield put(updateMarketPairs(tickerdata));
        console.log('tickerdata saga---->',tickerdata);
    }
    connection.onerror = evt => {
        console.error(evt);
    }
    // yield put(updateMarketPairs(tickerdata));
}



That's because foo is a generator and you cannot invoke it like an ordinary function.那是因为foo是一个生成器,你不能像普通的 function 那样调用它。 You need to use redux-saga call effect.你需要使用 redux-saga call效果。

But there is another problem: call effect won't work inside onmessage handler.但是还有另一个问题: call效果在onmessage处理程序中不起作用。 Special for cases like this, there is an eventChannel helper that allows saga to communicate with external event sources.特殊情况下,有一个eventChannel帮助器允许 saga 与外部事件源通信。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM