@@ -5,7 +5,7 @@ import List from '@mui/material/List';
5
5
import ListItem from '@mui/material/ListItem' ;
6
6
import ListItemText from '@mui/material/ListItemText' ;
7
7
import Button from '@mui/material/Button' ;
8
- import React , { useEffect } from 'react' ;
8
+ import React from 'react' ;
9
9
import { RootState } from '../../redux/store' ;
10
10
import TextField from '@mui/material/TextField' ;
11
11
import {
@@ -19,18 +19,17 @@ import {
19
19
setUserList
20
20
} from '../../redux/reducers/slice/roomSlice' ;
21
21
import { codePreviewCooperative } from '../../redux/reducers/slice/codePreviewSlice' ;
22
- import config from '../../../../config' ;
23
22
import { cooperativeStyle } from '../../redux/reducers/slice/styleSlice' ;
24
23
// websocket front end starts here
25
- import { io } from 'socket.io-client' ;
26
24
import store from '../../redux/store' ;
27
25
//pasted from navbarbuttons
28
- import debounce from '../../../../node_modules/lodash/debounce.js' ;
26
+ import {
27
+ initializeSocket ,
28
+ getSocket ,
29
+ disconnectSocket
30
+ } from '../../helperFunctions/socket' ;
29
31
30
- // // for websockets
31
- // // Part - join room and room code functionality
32
- let socket ;
33
- const { API_BASE_URL } = config ;
32
+ //Websocket
34
33
35
34
const RoomsContainer = ( ) => {
36
35
const dispatch = useDispatch ( ) ;
@@ -43,131 +42,70 @@ const RoomsContainer = () => {
43
42
) ;
44
43
45
44
function initSocketConnection ( roomCode : string ) {
46
- if ( socket ) socket . disconnect ( ) ; //edge case check if socket connection existed
47
- //establishing client and server connection
48
- socket = io ( API_BASE_URL , {
49
- transports : [ 'websocket' ]
50
- } ) ;
51
-
52
- //connecting user to server
53
- socket . on ( 'connect' , ( ) => {
54
- socket . emit ( 'joining' , userName , roomCode ) ;
55
- console . log ( `${ userName } Joined room ${ roomCode } ` ) ;
56
- } ) ;
57
-
58
- //If you are the host: send current state to server when a new user joins
59
- socket . on ( 'requesting state from host' , ( callback ) => {
60
- const newState = store . getState ( ) ; //pull the current state
61
- callback ( newState ) ; //send it to backend server
62
- } ) ;
63
-
64
- //If you are the new user: receive the state from the host
65
- socket . on ( 'server emitting state from host' , ( state , callback ) => {
66
- //dispatching new state to change user current state
67
- console . log ( 'state recieved by new join:' , state ) ;
68
- store . dispatch ( allCooperativeState ( state . appState ) ) ;
69
- store . dispatch ( codePreviewCooperative ( state . codePreviewCooperative ) ) ;
70
- store . dispatch ( cooperativeStyle ( state . styleSlice ) ) ;
71
- callback ( { status : 'confirmed' } ) ;
72
- } ) ;
73
-
74
- // update user list when there's a change: new join or leave the room
75
- socket . on ( 'updateUserList' , ( newUserList : object ) => {
76
- dispatch ( setUserList ( Object . values ( newUserList ) ) ) ;
77
- } ) ;
78
-
79
- // // receive the new state from the server and dispatch action creators to update state
80
- // socket.on('new state from back', (event) => {
81
- // const currentStore = JSON.parse(JSON.stringify(store.getState()));
82
- // const newState = JSON.parse(event);
83
-
84
- // const areStatesEqual = (stateA, stateB) =>
85
- // JSON.stringify(stateA) === JSON.stringify(stateB);
86
-
87
- // //checking if current state are equal to the state being sent from server
88
- // if (!areStatesEqual(currentStore, newState)) {
89
- // if (!areStatesEqual(currentStore.appState, newState.appState)) {
90
- // store.dispatch(allCooperativeState(newState.appState));
91
- // } else if (
92
- // !areStatesEqual(
93
- // currentStore.codePreviewSlice,
94
- // newState.codePreviewCooperative
95
- // )
96
- // ) {
97
- // store.dispatch(
98
- // codePreviewCooperative(newState.codePreviewCooperative)
99
- // );
100
- // } else if (
101
- // !areStatesEqual(currentStore.styleSlice, newState.styleSlice)
102
- // ) {
103
- // store.dispatch(cooperativeStyle(newState.styleSlice));
104
- // }
105
- // }
45
+ // if (socket) socket.disconnect(); //edge case: disconnect previous socket connection
46
+ // // establishing client and server connection
47
+ // socket = io(API_BASE_URL, {
48
+ // transports: ['websocket']
106
49
// });
107
- }
108
50
109
- // let previousState = store.getState();
110
- // // sending info to backend whenever the redux store changes
111
- // //handling state changes and send to server
51
+ initializeSocket ( ) ;
52
+ const socket = getSocket ( ) ;
53
+ // only create a new connection if not already connected
54
+ if ( socket ) {
55
+ //run everytime when a client connects to server
56
+ socket . on ( 'connect' , ( ) => {
57
+ socket . emit ( 'joining' , userName , roomCode ) ;
58
+ console . log ( `${ userName } Joined room ${ roomCode } from RoomsConatiner` ) ;
59
+ } ) ;
112
60
113
- // const findStateDiff = (prevState, newState) => {
114
- // const changes = {};
115
- // for (let key in newState) {
116
- // if (JSON.stringify(newState[key]) !== JSON.stringify(prevState[key])) {
117
- // changes[key] = newState[key];
118
- // }
119
- // }
120
- // return changes;
121
- // };
61
+ //If you are the host: send current state to server when a new user joins
62
+ socket . on ( 'requesting state from host' , ( callback ) => {
63
+ const newState = store . getState ( ) ; //pull the current state
64
+ callback ( newState ) ; //send it to backend server
65
+ } ) ;
122
66
123
- // const handleStoreChange = debounce(() => {
124
- // const newState = store.getState();
125
- // const roomCode = newState.roomSlice.roomCode;
126
- // const changes = findStateDiff(previousState, newState);
67
+ //If you are the new user: receive the state from the host
68
+ socket . on ( 'server emitting state from host' , ( state , callback ) => {
69
+ //dispatching new state to change user current state
70
+ console . log ( 'state recieved by new join:' , state ) ;
71
+ store . dispatch ( allCooperativeState ( state . appState ) ) ;
72
+ store . dispatch ( codePreviewCooperative ( state . codePreviewCooperative ) ) ;
73
+ store . dispatch ( cooperativeStyle ( state . styleSlice ) ) ;
74
+ callback ( { status : 'confirmed' } ) ;
75
+ } ) ;
127
76
128
- // if (Object.keys(changes).length > 0) {
129
- // // Send the current state to the server
130
- // console.log('newState:', newState);
131
- // console.log('changes:', changes);
132
- // socket.emit('new state from front', JSON.stringify(changes), roomCode);
133
- // //re-assgin previousState to be the newState
134
- // previousState = newState;
135
- // }
136
- // }, 100);
77
+ // update user list when there's a change: new join or leave the room
78
+ socket . on ( 'updateUserList' , ( newUserList ) => {
79
+ dispatch ( setUserList ( newUserList ) ) ;
80
+ } ) ;
137
81
138
- // //listening to changes from store by users, whenever the store's state changes, invoke handleStoreChange function
139
- // store.subscribe(() => {
140
- // if (socket) {
141
- // handleStoreChange( );
142
- // }
143
- // });
82
+ socket . on ( 'child data from server' , ( childData : object ) => {
83
+ console . log ( 'child data received by users' , childData ) ;
84
+ store . dispatch ( addChild ( childData ) ) ;
85
+ } ) ;
86
+ }
87
+ }
144
88
145
89
function handleUserEnteredRoom ( roomCode ) {
146
90
initSocketConnection ( roomCode ) ;
147
91
}
148
92
149
- //-----------------------
150
-
151
- if ( socket ) {
152
- socket . on ( 'child data from back' , ( childData : string ) => {
153
- console . log ( 'child data received by users' , JSON . parse ( childData ) ) ;
154
- store . dispatch ( addChild ( JSON . parse ( childData ) ) ) ;
155
- } ) ;
156
- }
157
-
158
- //-----------------------
159
-
160
93
//joining room function
161
94
function joinRoom ( ) {
162
- if ( userList . length !== 0 ) dispatch ( setUserList ( [ ] ) ) ; //edge case check if userList not empty.
163
- handleUserEnteredRoom ( roomCode ) ; // Call handleUserEnteredRoom when joining a room
164
- dispatch ( setRoomCode ( roomCode ) ) ;
95
+ //edge case: if userList is not empty, reset it to empty array
96
+ if ( userList . length !== 0 ) dispatch ( setUserList ( [ ] ) ) ;
97
+ handleUserEnteredRoom ( roomCode ) ;
98
+
99
+ dispatch ( setRoomCode ( roomCode ) ) ; //?
165
100
dispatch ( setUserJoined ( true ) ) ; //setting joined room to true for rendering leave room button
166
101
}
167
102
168
103
function leaveRoom ( ) {
104
+ let socket = getSocket ( ) ;
169
105
if ( socket ) {
170
106
socket . disconnect ( ) ; //disconnecting socket from server
107
+ socket = null ;
108
+ console . log ( 'user leaves the room' ) ;
171
109
}
172
110
//reset all state values
173
111
dispatch ( setRoomCode ( '' ) ) ;
0 commit comments