@@ -31,53 +31,74 @@ let socket;
31
31
const { API_BASE_URL } = config ;
32
32
const RoomsContainer = ( ) => {
33
33
const dispatch = useDispatch ( ) ;
34
- const { state , roomCode, userName, userList, userJoined } = useSelector (
34
+ const { roomCode, userName, userList, userJoined } = useSelector (
35
35
( store : RootState ) => ( {
36
- state : store . appState ,
37
36
roomCode : store . roomSlice . roomCode ,
38
37
userName : store . roomSlice . userName ,
39
38
userList : store . roomSlice . userList ,
40
39
userJoined : store . roomSlice . userJoined
41
40
} )
42
41
) ;
43
- React . useEffect ( ( ) => {
44
- console . log ( 'You Joined Room---front end:' , roomCode ) ;
45
- } , [ roomCode ] ) ;
46
42
47
- function initSocketConnection ( roomCode ) {
43
+ function initSocketConnection ( roomCode : string ) {
48
44
if ( socket ) socket . disconnect ( ) ; //edge case check if socket connection existed
49
45
50
46
socket = io ( API_BASE_URL , {
47
+ //establishing client and server connection
51
48
transports : [ 'websocket' ]
52
49
} ) ;
53
50
51
+ //connecting user to server
54
52
socket . on ( 'connect' , ( ) => {
55
- console . log ( `You Connected With Id: ${ socket . id } ` ) ;
56
- socket . emit ( 'join-room' , roomCode ) ; // Join the room when connected
57
- console . log ( `Your Nickname Is: ${ userName } ` ) ;
58
- //passing current client nickname to server
59
- socket . emit ( 'userJoined' , userName , roomCode ) ;
60
- //listening to back end for updating user list
61
- socket . on ( 'updateUserList' , ( newUserList ) => {
62
- dispatch ( setUserList ( Object . values ( newUserList ) ) ) ;
63
- } ) ;
53
+ socket . emit ( 'joining' , userName , roomCode ) ;
54
+ console . log ( `${ userName } Joined room ${ roomCode } ` ) ;
55
+ } ) ;
56
+
57
+ //send state from host to room when new user joins
58
+ socket . on ( 'requesting state from host' , ( callback ) => {
59
+ //getting state request from user from back end
60
+ const newState = store . getState ( ) ;
61
+ callback ( newState ) ; //pull new state from host and send it to back end
62
+ } ) ;
63
+
64
+ socket . on ( 'server emitting state from host' , ( state , callback ) => {
65
+ //getting state from host once joined a room
66
+ //dispatching new state to change user current state
67
+ store . dispatch ( allCooperativeState ( state . appState ) ) ;
68
+ store . dispatch ( codePreviewCooperative ( state . codePreviewCooperative ) ) ;
69
+ store . dispatch ( cooperativeStyle ( state . styleSlice ) ) ;
70
+ callback ( { status : 'confirmed' } ) ;
71
+ } ) ;
72
+
73
+ //listening to back end for updating user list
74
+ socket . on ( 'updateUserList' , ( newUserList : object ) => {
75
+ dispatch ( setUserList ( Object . values ( newUserList ) ) ) ;
64
76
} ) ;
65
77
66
78
// receiving the message from the back end
67
- socket . on ( 'receive message' , ( event ) => {
68
- let currentStore : any = JSON . stringify ( store . getState ( ) ) ;
69
- // console.log('event ', event);
70
- if ( currentStore !== event ) {
71
- currentStore = JSON . parse ( currentStore ) ;
72
- event = JSON . parse ( event ) ;
73
- if ( currentStore . appState !== event . appState ) {
74
- store . dispatch ( allCooperativeState ( event . appState ) ) ;
79
+ socket . on ( 'new state from back' , ( event ) => {
80
+ const currentStore = JSON . parse ( JSON . stringify ( store . getState ( ) ) ) ;
81
+ const parsedEvent = JSON . parse ( event ) ;
82
+
83
+ const areStatesEqual = ( stateA , stateB ) =>
84
+ JSON . stringify ( stateA ) === JSON . stringify ( stateB ) ;
85
+
86
+ if ( ! areStatesEqual ( currentStore , parsedEvent ) ) {
87
+ if ( ! areStatesEqual ( currentStore . appState , parsedEvent . appState ) ) {
88
+ store . dispatch ( allCooperativeState ( parsedEvent . appState ) ) ;
89
+ } else if (
90
+ ! areStatesEqual (
91
+ currentStore . codePreviewSlice ,
92
+ parsedEvent . codePreviewCooperative
93
+ )
94
+ ) {
95
+ store . dispatch (
96
+ codePreviewCooperative ( parsedEvent . codePreviewCooperative )
97
+ ) ;
75
98
} else if (
76
- currentStore . codePreviewSlice !== event . codePreviewCooperative
99
+ ! areStatesEqual ( currentStore . styleSlice , parsedEvent . styleSlice )
77
100
) {
78
- store . dispatch ( codePreviewCooperative ( event . codePreviewCooperative ) ) ;
79
- } else if ( currentStore . styleSlice !== event . styleSlice ) {
80
- store . dispatch ( cooperativeStyle ( event . styleSlice ) ) ;
101
+ store . dispatch ( cooperativeStyle ( parsedEvent . styleSlice ) ) ;
81
102
}
82
103
}
83
104
} ) ;
@@ -89,24 +110,26 @@ const RoomsContainer = () => {
89
110
90
111
let previousState = store . getState ( ) ;
91
112
// sending info to backend whenever the redux store changes
113
+ //handling state changes and send to server
92
114
const handleStoreChange = debounce ( ( ) => {
93
115
const newState = store . getState ( ) ;
94
116
const roomCode = newState . roomSlice . roomCode ;
95
117
96
118
if ( JSON . stringify ( newState ) !== JSON . stringify ( previousState ) ) {
97
119
// Send the current state to the server
98
- console . log ( 'emit state unequal' ) ;
99
- socket . emit ( 'custom-event' , JSON . stringify ( newState ) , roomCode ) ;
120
+ socket . emit ( 'new state from front' , JSON . stringify ( newState ) , roomCode ) ;
100
121
previousState = newState ;
101
122
}
102
123
} , 100 ) ;
103
124
125
+ //listening to changes from store from user, invoke handle store change.
104
126
store . subscribe ( ( ) => {
105
127
if ( socket ) {
106
128
handleStoreChange ( ) ;
107
129
}
108
130
} ) ;
109
131
132
+ //joining room function
110
133
function joinRoom ( ) {
111
134
if ( userList . length !== 0 ) dispatch ( setUserList ( [ ] ) ) ; //edge case check if userList not empty.
112
135
handleUserEnteredRoom ( roomCode ) ; // Call handleUserEnteredRoom when joining a room
@@ -116,24 +139,20 @@ const RoomsContainer = () => {
116
139
117
140
function leaveRoom ( ) {
118
141
if ( socket ) {
119
- socket . emit ( 'updateUserDisconnect' , roomCode ) ;
120
- socket . disconnect ( ) ;
121
- } //disconnecting socket functionality
142
+ socket . disconnect ( ) ; //disconnecting socket from server
143
+ }
144
+ //reset all state values
122
145
dispatch ( setRoomCode ( '' ) ) ;
123
146
dispatch ( setUserName ( '' ) ) ;
124
147
dispatch ( setUserList ( [ ] ) ) ;
125
148
dispatch ( setUserJoined ( false ) ) ; //setting joined to false so join button appear
126
149
}
127
150
128
- //checking if both text field have any input (not including spaces)
129
- function checkInputField ( ...inputs : any ) {
130
- let userName : String = inputs [ 0 ] . trim ( ) ;
131
- let roomCode : String = inputs [ 1 ] . trim ( ) ;
132
- if ( userName . length !== 0 && roomCode . length !== 0 ) {
133
- return false ;
134
- } else {
135
- return true ;
136
- }
151
+ //checking empty input field (not including spaces)
152
+ function checkInputField ( ...inputs ) {
153
+ let userName : string = inputs [ 0 ] . trim ( ) ;
154
+ let roomCode : string = inputs [ 1 ] . trim ( ) ;
155
+ return userName . length === 0 || roomCode . length === 0 ;
137
156
}
138
157
139
158
return (
@@ -173,8 +192,7 @@ const RoomsContainer = () => {
173
192
< Typography
174
193
variant = "body1"
175
194
sx = { {
176
- color : 'white' , // Text color for the count
177
- borderRadius : 4 // Optional: Add rounded corners
195
+ color : 'white' // Text color for the count
178
196
} }
179
197
>
180
198
Users: { userList . length }
@@ -208,14 +226,13 @@ const RoomsContainer = () => {
208
226
) : (
209
227
//after joinning room
210
228
< >
211
- < > </ >
212
229
< TextField
213
230
hiddenLabel = { true }
214
231
id = "filled-hidden-label-small"
215
232
variant = "filled"
216
233
size = "small"
217
234
value = { userName }
218
- placeholder = "Input nickname "
235
+ placeholder = "Input Nickname "
219
236
onChange = { ( e ) => dispatch ( setUserName ( e . target . value ) ) }
220
237
/>
221
238
< TextField
0 commit comments