Answer the question
In order to leave comments, you need to log in
Websocket + redux, how to properly hack?
Good afternoon, please tell me how to properly connect WebSocket to Redax.
Standard example, at the level of receiving data from the API.
export const GET_DIR = 'GET_DIR';
const ws = new WebSocket('wss://ADDRESS/');
ws.onopen = () => {
console.log('WS Open!');
};
ws.onclose = () => {
console.log('WS Close');
};
// ws.onmessage = (data) => {
// console.log("Getting Data: ", JSON.parse(data.data));
// return data.data;
// }
export function getDir() {
return function (data, dispatch) {
console. log("Get path -> ");
method: 'KeyStore',
args: ['S12', 'P12', ''],
};
ws.send(JSON.stringify(browseKeyStore));
return dispatch({
type: GET_DIR,
currentDirectory: 'DATA',
});
};
}
At what level after the request (ws.send(...), having received the data in ws.onmessage(data)) to push them into the dispatcher - into payload, and I need to take into account that the next call to ws.send(...) - will place in ws.onmessage(data) - other data that will need to be processed differently.
Answer the question
In order to leave comments, you need to log in
You answered your own question: ws.onmessage(data)
Inside the onmessage handler, depending on what came in data - you can process your data in different ways. In order for the processed data to get to the right reducer, you need to dispatch different actions.
For example:
ws.onmessage = (data) => {
const myData = JSON.parse(data.data); // предположим, что там собаки или кошки
if (data.data = 'cats') { dispatch(addToCats(data.data) }
if (... )
// можете обработать каким-либо образом:
const newData = '123' + data.data
dispatch(anyActionCreator(newData)
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question