show status of sockets independently
This commit is contained in:
		
							parent
							
								
									dd33ec59ad
								
							
						
					
					
						commit
						3c9192ab3a
					
				
					 3 changed files with 32 additions and 26 deletions
				
			
		| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
import useWebSocket, {ReadyState} from 'react-use-websocket';
 | 
					import useWebSocket from 'react-use-websocket';
 | 
				
			||||||
import {useEffect, useRef} from 'react';
 | 
					import {useEffect, useRef} from 'react';
 | 
				
			||||||
import './ClientScreen.css';
 | 
					import './ClientScreen.css';
 | 
				
			||||||
 | 
					import {statusTextForReadyState} from './statusTextForReadyState.tsx';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const pixelsPerRow = 352;
 | 
					const pixelsPerRow = 352;
 | 
				
			||||||
const pixelsPerCol = 160;
 | 
					const pixelsPerCol = 160;
 | 
				
			||||||
| 
						 | 
					@ -47,7 +48,6 @@ function drawPixelsToCanvas(pixels: Uint8Array, canvas: HTMLCanvasElement) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    drawContext.putImageData(imageData, 0, 0);
 | 
					    drawContext.putImageData(imageData, 0, 0);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					 | 
				
			||||||
export default function ClientScreen({}: {}) {
 | 
					export default function ClientScreen({}: {}) {
 | 
				
			||||||
    const canvasRef = useRef<HTMLCanvasElement>(null);
 | 
					    const canvasRef = useRef<HTMLCanvasElement>(null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -74,16 +74,8 @@ export default function ClientScreen({}: {}) {
 | 
				
			||||||
        sendMessage('');
 | 
					        sendMessage('');
 | 
				
			||||||
    }, [lastMessage, canvasRef.current]);
 | 
					    }, [lastMessage, canvasRef.current]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    const connectionStatus = {
 | 
					 | 
				
			||||||
        [ReadyState.CONNECTING]: 'Connecting',
 | 
					 | 
				
			||||||
        [ReadyState.OPEN]: 'Open',
 | 
					 | 
				
			||||||
        [ReadyState.CLOSING]: 'Closing',
 | 
					 | 
				
			||||||
        [ReadyState.CLOSED]: 'Closed',
 | 
					 | 
				
			||||||
        [ReadyState.UNINSTANTIATED]: 'Uninstantiated',
 | 
					 | 
				
			||||||
    }[readyState];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    return <>
 | 
					    return <>
 | 
				
			||||||
        <span>The WebSocket is currently {connectionStatus}</span>
 | 
					        <span>The screen is currently {statusTextForReadyState[readyState]}</span>
 | 
				
			||||||
        <canvas ref={canvasRef} id="screen" width={pixelsPerRow} height={pixelsPerCol}/>
 | 
					        <canvas ref={canvasRef} id="screen" width={pixelsPerRow} height={pixelsPerCol}/>
 | 
				
			||||||
    </>;
 | 
					    </>;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1,6 +1,7 @@
 | 
				
			||||||
import './Controls.css';
 | 
					import './Controls.css';
 | 
				
			||||||
import useWebSocket from 'react-use-websocket';
 | 
					import useWebSocket from 'react-use-websocket';
 | 
				
			||||||
import {useEffect} from 'react';
 | 
					import {useEffect} from 'react';
 | 
				
			||||||
 | 
					import {statusTextForReadyState} from './statusTextForReadyState.tsx';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default function Controls({playerId}: {
 | 
					export default function Controls({playerId}: {
 | 
				
			||||||
    playerId: string
 | 
					    playerId: string
 | 
				
			||||||
| 
						 | 
					@ -9,7 +10,8 @@ export default function Controls({playerId}: {
 | 
				
			||||||
    url.searchParams.set('playerId', playerId);
 | 
					    url.searchParams.set('playerId', playerId);
 | 
				
			||||||
    const {
 | 
					    const {
 | 
				
			||||||
        sendMessage,
 | 
					        sendMessage,
 | 
				
			||||||
        getWebSocket
 | 
					        getWebSocket,
 | 
				
			||||||
 | 
					        readyState
 | 
				
			||||||
    } = useWebSocket(url.toString(), {
 | 
					    } = useWebSocket(url.toString(), {
 | 
				
			||||||
        shouldReconnect: () => true,
 | 
					        shouldReconnect: () => true,
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
| 
						 | 
					@ -56,7 +58,9 @@ export default function Controls({playerId}: {
 | 
				
			||||||
        };
 | 
					        };
 | 
				
			||||||
    }, [sendMessage]);
 | 
					    }, [sendMessage]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return <div className="controls">
 | 
					    return <>
 | 
				
			||||||
 | 
					        <span>The controller is currently {statusTextForReadyState[readyState]}</span>
 | 
				
			||||||
 | 
					        <div className="controls">
 | 
				
			||||||
            <div className="control">
 | 
					            <div className="control">
 | 
				
			||||||
                <div className="row">
 | 
					                <div className="row">
 | 
				
			||||||
                    <kbd className="up">▲</kbd>
 | 
					                    <kbd className="up">▲</kbd>
 | 
				
			||||||
| 
						 | 
					@ -72,5 +76,6 @@ export default function Controls({playerId}: {
 | 
				
			||||||
                <kbd className="space">Space</kbd>
 | 
					                <kbd className="space">Space</kbd>
 | 
				
			||||||
                <h3>Fire</h3>
 | 
					                <h3>Fire</h3>
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
    </div>;
 | 
					        </div>
 | 
				
			||||||
 | 
					    </>;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										9
									
								
								tank-frontend/src/statusTextForReadyState.tsx
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								tank-frontend/src/statusTextForReadyState.tsx
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
					@ -0,0 +1,9 @@
 | 
				
			||||||
 | 
					import {ReadyState} from 'react-use-websocket';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export const statusTextForReadyState = {
 | 
				
			||||||
 | 
					    [ReadyState.CONNECTING]: 'Connecting',
 | 
				
			||||||
 | 
					    [ReadyState.OPEN]: 'Open',
 | 
				
			||||||
 | 
					    [ReadyState.CLOSING]: 'Closing',
 | 
				
			||||||
 | 
					    [ReadyState.CLOSED]: 'Closed',
 | 
				
			||||||
 | 
					    [ReadyState.UNINSTANTIATED]: 'Uninstantiated',
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue