Merge branch 'master' of http://khuhub.khu.ac.kr/2020105578/nodejs-game into master
Showing
3 changed files
with
64 additions
and
17 deletions
1 | import React from 'react'; | 1 | import React from 'react'; |
2 | +import { BrowserRouter as Router, Switch, Route } from 'react-router-dom'; | ||
3 | +import { socket, SocketProvider } from './SocketContext'; | ||
4 | +import { Login } from './Login'; | ||
2 | 5 | ||
3 | -function App() { | 6 | +const App: React.FC = () => { |
4 | return ( | 7 | return ( |
5 | - <div className="App"> | 8 | + <SocketProvider value={socket}> |
6 | - <div className="ml-3 mb-3 font-bold text-2xl">TESTING</div> | 9 | + <Router> |
7 | - <header className="App-header"> | 10 | + <Switch> |
8 | - <p> | 11 | + <Route exact path='/' component={Login}/> |
9 | - Edit <code>src/App.tsx</code> and save to reload. | 12 | + <Route path='/rooms'></Route> |
10 | - </p> | 13 | + <Route path='/:roomId'></Route> |
11 | - <a | 14 | + </Switch> |
12 | - className="App-link" | 15 | + </Router> |
13 | - href="https://reactjs.org" | 16 | + </SocketProvider> |
14 | - target="_blank" | ||
15 | - rel="noopener noreferrer" | ||
16 | - > | ||
17 | - Learn React | ||
18 | - </a> | ||
19 | - </header> | ||
20 | - </div> | ||
21 | ); | 17 | ); |
22 | } | 18 | } |
23 | 19 | ... | ... |
web/src/Login.tsx
0 → 100644
1 | +import React, { useContext, useState } from 'react'; | ||
2 | +import { RouteComponentProps } from 'react-router'; | ||
3 | +import SocketContext from './SocketContext'; | ||
4 | + | ||
5 | +export const Login: React.FC<RouteComponentProps> = ({ history }) => { | ||
6 | + const socket = useContext(SocketContext); | ||
7 | + const [ username, setUsername ] = useState(""); | ||
8 | + | ||
9 | + const login = () => { | ||
10 | + socket.emit('LoginMessage', username, ({ ok } : { ok: boolean }) => { | ||
11 | + if (ok) { | ||
12 | + history.push('/rooms'); | ||
13 | + } else { | ||
14 | + console.error('login error!'); // TODO: 팝업 에러? | ||
15 | + } | ||
16 | + }); | ||
17 | + } | ||
18 | + | ||
19 | + return ( | ||
20 | + <div> | ||
21 | + <header>Header</header> | ||
22 | + <div className="flex items-center"> | ||
23 | + <input type="text" | ||
24 | + placeholder="Username" | ||
25 | + className="px-3 py-2 bg-white | ||
26 | + placeholder-gray-300 text-gray-700 text-sm | ||
27 | + rounded shadow outline-none focus:outline-none" | ||
28 | + value={username} | ||
29 | + onChange={e => setUsername(e.target.value)} | ||
30 | + /> | ||
31 | + <button className="bg-green-500 active:bg-green-600 | ||
32 | + text-white font-bold uppercase text-sm | ||
33 | + px-5 py-2 ml-3 rounded shadow hover:shadow-lg | ||
34 | + outline-none focus:outline-none | ||
35 | + ease-linear transition-all duration-150" | ||
36 | + type="button" | ||
37 | + onClick={() => login()}>Login</button> | ||
38 | + </div> | ||
39 | + <footer>Footer</footer> | ||
40 | + </div> | ||
41 | + ) | ||
42 | +} |
web/src/SocketContext.ts
0 → 100644
1 | +import React from 'react'; | ||
2 | +import { io } from 'socket.io-client'; | ||
3 | + | ||
4 | +export const socket = io('http://localhost/'); | ||
5 | +const SocketContext = React.createContext(socket); | ||
6 | + | ||
7 | +export const SocketProvider = SocketContext.Provider; | ||
8 | +export const SocketConsumer = SocketContext.Consumer; | ||
9 | +export default SocketContext; |
-
Please register or login to post a comment