Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web sockets #29

Merged
merged 11 commits into from
Apr 6, 2023
Prev Previous commit
Next Next commit
Added relevant client state for sockets
  • Loading branch information
mjhuff committed Apr 6, 2023
commit 65183d724d46c0f132b441feaa04835f2a74406d
21 changes: 12 additions & 9 deletions client/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,42 @@ import { RemoteContainer } from './components/RemoteContainer';
import MigrationButton from './components/MigrationButton';
import Overlay from './components/Overlay';
import { socket } from './socket';
import {
updateDataTransferProgressPercent,
updateSocketConnectivity
} from './slice';
//import styles if necessary
//may need to import functions from slices here

const App = (props) => {
const dispatch = useDispatch();
const { isMigrating, origin, destination } = useSelector(
(state) => state.GUI
);

const [isConnected, setIsConnected] = useState(socket.connected);
const [fooEvents, setFooEvents] = useState([]);

//Basic client socket.io connection.
//MAY NEED TO SHIFT THIS LOGIC INTO THE COMPONENT WE NEED.
useEffect(() => {
function onConnect() {
setIsConnected(true);
dispatch(updateSocketConnectivity(true));
}

function onDisconnect() {
setIsConnected(false);
dispatch(updateSocketConnectivity(false));
}

function onFooEvent(value) {
setFooEvents((previous) => [...previous, value]);
function onDataTransfer(value) {
dispatch(updateDataTransferProgressPercent(value));
}

socket.on('connect', onConnect);
socket.on('disconnect', onDisconnect);
socket.on('foo', onFooEvent);
socket.on('data transfer', onDataTransfer);

return () => {
socket.off('connect', onConnect);
socket.off('disconnect', onDisconnect);
socket.off('foo', onFooEvent);
socket.off('data transfer', onDataTransfer);
};
}, []);

Expand Down
19 changes: 15 additions & 4 deletions client/slice.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const slice = createSlice({
accountId: '',
selectedBucket: '',
service: '',
bucketOptions: [],
bucketOptions: []
},
destination: {
name: '',
Expand All @@ -22,8 +22,12 @@ const slice = createSlice({
accountId: '',
selectedBucket: '',
service: '',
bucketOptions: [],
bucketOptions: []
},
socket: {
isConnected: false,
dataTransferProgressPercent: ''
}
},
reducers: {
migrationStatusChange: (state, action) => {
Expand All @@ -46,7 +50,6 @@ const slice = createSlice({
state.destination = action.payload.destination;
},
updateDestinationSecretKey: (state, action) => {

state.destination = action.payload.destination;
},
updateAccountId: (state, action) => {
Expand All @@ -65,6 +68,12 @@ const slice = createSlice({
},
updateErrorState: (state, action) => {
state.errState = action.payload;
},
updateSocketConnectivity: (state, action) => {
state.socket.isConnected = action.payload;
},
updateDataTransferProgressPercent: (state, action) => {
state.socket.dataTransferProgressPercent = action.payload;
}
},
extraReducers: (builder) => {
Expand All @@ -91,5 +100,7 @@ export const {
updateOriginBuckets,
updateDestinationBuckets,
updateSelectedBucket,
updateErrorState
updateErrorState,
updateSocketConnectivity,
updateDataTransferProgressPercent
} = slice.actions;
1 change: 1 addition & 0 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const io = new Server(server, {
});

//PUT ALL THE WEBSOCKET LOGIC HERE FOR NOW.
//CURRENT PROBLEM: I DON'T KNOW HOW TO EXPORT THIS STUFF.
io.on('connection', (socket) => {
console.log('a user connected');
});
Expand Down
6 changes: 6 additions & 0 deletions server/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { io } = require('./server');

//PUT ALL THE WEBSOCKET LOGIC HERE FOR NOW.
io.on('connection', (socket) => {
console.log('a user connected');
});