Remove duplicate socket connection

pull/9/head
MiguelMLorente 2024-12-04 22:55:40 +01:00
parent fae67ce2a2
commit 2191a2fdc4
3 changed files with 26 additions and 18 deletions

View File

@ -1,15 +1,17 @@
import React from "react"; import React from "react";
import { io } from "socket.io-client"; import { Socket } from "socket.io-client";
import LandingPage from "./pages/landing/LandingPage"; import LandingPage from "./pages/landing/LandingPage";
function App() { export interface AppProps {
const socket = io("http://localhost:3010"); socket: Socket;
return (
<div className="App">
<LandingPage socket={socket} />
</div>
);
} }
const App = (props: AppProps) => {
return (
<div className="app">
<LandingPage {...props} />
</div>
);
};
export default App; export default App;

View File

@ -2,12 +2,14 @@ import React from "react";
import ReactDOM from "react-dom/client"; import ReactDOM from "react-dom/client";
import App from "./App"; import App from "./App";
import "./App.scss"; import "./App.scss";
import { io } from "socket.io-client";
const root = ReactDOM.createRoot( const root = ReactDOM.createRoot(
document.getElementById("root") as HTMLElement, document.getElementById("root") as HTMLElement,
); );
const socket = io("http://localhost:3010");
root.render( root.render(
<React.StrictMode> <React.StrictMode>
<App /> <App socket={socket} />
</React.StrictMode>, </React.StrictMode>,
); );

View File

@ -7,8 +7,12 @@ import {
import React, { ChangeEvent } from "react"; import React, { ChangeEvent } from "react";
import { Socket } from "socket.io-client"; import { Socket } from "socket.io-client";
const LandingPage = (props: { socket: Socket }) => { export interface LandingPageProps {
const socket = props.socket; socket: Socket;
}
const LandingPage = (props: LandingPageProps) => {
const { socket } = props;
const createLobbyPayload: CreateLobbyEvent = { userName: "" }; const createLobbyPayload: CreateLobbyEvent = { userName: "" };
const joinLobbyPayload: JoinLobbyEvent = { userName: "", lobbyId: "" }; const joinLobbyPayload: JoinLobbyEvent = { userName: "", lobbyId: "" };
@ -21,25 +25,25 @@ const LandingPage = (props: { socket: Socket }) => {
return ( return (
<React.Fragment> <React.Fragment>
<div className="LandingPageTitle"> <div className="landing-page-title">
<h1>Trains And Roads</h1> <h1>Trains And Roads</h1>
</div> </div>
<div className="UserNameImput"> <div className="user-name-input">
<input placeholder="Enter username" onChange={registerUsername}></input> <input placeholder="Enter username" onChange={registerUsername}></input>
</div> </div>
<div className="CreateLobbyButton"> <div className="create-lobby-button">
<button onClick={() => handleCreateLobby(socket, createLobbyPayload)}> <button onClick={() => handleCreateLobby(socket, createLobbyPayload)}>
Create Lobby Create Lobby
</button> </button>
</div> </div>
<div className="JoinLobby"> <div className="join-lobby">
<input <input
className="LobbyIdInput" className="lobby-id-input"
placeholder="Enter Lobby Id" placeholder="Enter Lobby Id"
onChange={registerLobbyId} onChange={registerLobbyId}
></input> ></input>
<button <button
className="JoinLobbyButton secondary" className="join-lobby-button secondary"
onClick={() => handleJoinLobby(socket, joinLobbyPayload)} onClick={() => handleJoinLobby(socket, joinLobbyPayload)}
> >
Join Lobby Join Lobby