Files copy and paste from store atoms and selector to redux slices folder
This commit is contained in:
29
redux/middleware/websocketMiddleware.js
Normal file
29
redux/middleware/websocketMiddleware.js
Normal file
@@ -0,0 +1,29 @@
|
||||
// /redux/websocketMiddleware.js
|
||||
const websocketMiddleware = () => {
|
||||
let socket;
|
||||
|
||||
return ({ dispatch }) =>
|
||||
(next) =>
|
||||
(action) => {
|
||||
if (action.type === "WS_CONNECT") {
|
||||
socket = new WebSocket(action.payload.url);
|
||||
|
||||
socket.onmessage = (event) => {
|
||||
const data = JSON.parse(event.data);
|
||||
dispatch({ type: "WS_MESSAGE_RECEIVED", payload: data });
|
||||
};
|
||||
|
||||
socket.onclose = () => {
|
||||
dispatch({ type: "WS_DISCONNECTED" });
|
||||
};
|
||||
}
|
||||
|
||||
if (action.type === "WS_DISCONNECT" && socket) {
|
||||
socket.close();
|
||||
}
|
||||
|
||||
return next(action);
|
||||
};
|
||||
};
|
||||
|
||||
export default websocketMiddleware;
|
||||
Reference in New Issue
Block a user