@@ -16,7 +16,7 @@ import './styles.css';
1616import { E44o5 } from './components/error' ;
1717
1818// Dynamically construct the socket url based on `window.location`
19- const socket = new WebSocket (
19+ let socket = new WebSocket (
2020 `${ window . location . protocol === 'https:' ? 'wss:' : 'ws:' } //${
2121 window . location . host
2222 } `
@@ -30,6 +30,7 @@ const App = () => {
3030
3131 const [ lessonNumber , setLessonNumber ] = useState ( 1 ) ;
3232 const [ description , setDescription ] = useState ( '' ) ;
33+ const [ locale , setLocale ] = useState ( 'english' ) ;
3334 const [ tests , setTests ] = useState < TestType [ ] > ( [ ] ) ;
3435 const [ hints , setHints ] = useState < string [ ] > ( [ ] ) ;
3536 const [ cons , setCons ] = useState < ConsoleError [ ] > ( [ ] ) ;
@@ -40,9 +41,10 @@ const App = () => {
4041 const [ debouncers , setDebouncers ] = useState < string [ ] > ( [ ] ) ;
4142 const [ connected , setConnected ] = useState < boolean > ( false ) ;
4243
43- useEffect ( ( ) => {
44+ function connectToWebSocket ( ) {
4445 socket . onopen = function ( _event ) {
4546 setConnected ( true ) ;
47+ setAlertCamper ( null ) ;
4648 sock ( Events . CONNECT ) ;
4749 } ;
4850 socket . onmessage = function ( event ) {
@@ -54,13 +56,24 @@ const App = () => {
5456 socket . onclose = function ( _event ) {
5557 setAlertCamper ( 'Client has disconnected from local server' ) ;
5658 setConnected ( false ) ;
59+ // Try to reconnect
60+ setTimeout ( ( ) => {
61+ socket = new WebSocket (
62+ `${ window . location . protocol === 'https:' ? 'wss:' : 'ws:' } //${
63+ window . location . host
64+ } `
65+ ) ;
66+ connectToWebSocket ( ) ;
67+ } , 1000 ) ;
5768 } ;
5869
5970 return ( ) => {
6071 console . log ( 'socket closing' ) ;
6172 socket . close ( ) ;
6273 } ;
63- } , [ ] ) ;
74+ }
75+
76+ useEffect ( connectToWebSocket , [ ] ) ;
6477
6578 const handle = {
6679 'handle-project-finish' : handleProjectFinish ,
@@ -76,6 +89,7 @@ const App = () => {
7689 'update-freeCodeCamp-config' : setFreeCodeCampConfig ,
7790 'update-error' : updateError ,
7891 'reset-tests' : resetTests ,
92+ 'update-locale' : setLocale ,
7993 RESPONSE : debounce
8094 } ;
8195
@@ -182,7 +196,7 @@ const App = () => {
182196 if ( alertCamper ) {
183197 return (
184198 < >
185- < Header updateProject = { updateProject } />
199+ < Header { ... { sock , updateProject, freeCodeCampConfig } } />
186200 < E44o5 text = { alertCamper } error = { error } />
187201 </ >
188202 ) ;
@@ -191,7 +205,7 @@ const App = () => {
191205 return (
192206 < >
193207 < Suspense fallback = { < Loader /> } >
194- < Header updateProject = { updateProject } />
208+ < Header { ... { sock , updateProject, freeCodeCampConfig } } />
195209 { project ? (
196210 < Project
197211 { ...{
@@ -210,7 +224,7 @@ const App = () => {
210224 } }
211225 />
212226 ) : (
213- < Landing { ...{ sock, projects, freeCodeCampConfig } } />
227+ < Landing { ...{ locale , sock, projects, freeCodeCampConfig } } />
214228 ) }
215229 </ Suspense >
216230 </ >
0 commit comments