Toggle navigation
Toggle navigation
This project
Loading...
Sign in
강동현
/
nodejs-game
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
Overnap
2021-06-06 01:56:18 +0900
Browse Files
Options
Browse Files
Download
Plain Diff
Commit
98a313f8af7fb5be01a835dcc29f3bfd6c58e233
98a313f8
2 parents
46d1ac21
3fd2bc51
Merge branch 'develop' into feature/room
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
41 additions
and
41 deletions
web/package.json
web/src/components/common/types.ts
web/src/components/rooms/RoomInfo.tsx → web/src/components/rooms/RoomBlock.tsx
web/src/contexts/SocketContext.ts
web/src/pages/Login.tsx
web/src/pages/Rooms.tsx
web/package.json
View file @
98a313f
...
...
@@ -23,7 +23,7 @@
"web-vitals"
:
"^1.0.1"
},
"scripts"
:
{
"start"
:
"npm run twcss && react-scripts start"
,
"start"
:
"npm run twcss &&
set PORT=3001 &&
react-scripts start"
,
"build"
:
"npm run twcss && react-scripts build"
,
"test"
:
"react-scripts test"
,
"eject"
:
"react-scripts eject"
,
...
...
web/src/components/common/types.ts
View file @
98a313f
...
...
@@ -4,10 +4,15 @@ export interface MessageResponse<T> {
result
?:
T
;
}
export
interface
RawMessage
{
type
:
string
;
message
:
any
;
}
export
const
MessageType
=
{
LOGIN
:
"login"
,
ROOM_LIST_REQUEST
:
"room
_list_reque
st"
,
ROOM_JOIN
:
"
room_join
"
,
ROOM_LIST_REQUEST
:
"room
Li
st"
,
ROOM_JOIN
:
"
joinRoom
"
,
ROOM_LEAVE
:
"room_leave"
,
ROOM_USER_UPDATE
:
"room_user_update"
,
ROOM_CHAT
:
"room_chat"
,
...
...
web/src/components/rooms/Room
Info
.tsx
→
web/src/components/rooms/Room
Block
.tsx
View file @
98a313f
import React, { useContext } from 'react';
import React, { useC
allback, useC
ontext } from 'react';
import { useHistory } from 'react-router';
import SocketContext from '../../contexts/SocketContext';
import { MessageResponse, MessageType } from '../common/types';
import { MessageResponse, MessageType
, RawMessage
} from '../common/types';
import { RoomData } from '../room/types';
import { Room } from './types';
interface RoomProps {
interface Room
Block
Props {
room: Room
}
export const Room
Info: React.FC<Room
Props> = ({ room }) => {
export const Room
Block: React.FC<RoomBlock
Props> = ({ room }) => {
const history = useHistory();
const socket = useContext(SocketContext);
const joinRoom = () => {
const joinRoom =
useCallback(
() => {
if (room.currentUsers < room.maxUsers) {
socket.emit(MessageType.ROOM_JOIN, (response: MessageResponse<RoomData>) => {
const rawMessage: RawMessage = {
type: MessageType.ROOM_JOIN,
message: { uuid: room.uuid }
}
socket.emit('msg', rawMessage, (response: MessageResponse<RoomData>) => {
if (response.ok) {
history.push({
pathname: '/' + room.uuid,
...
...
@@ -27,7 +31,7 @@ export const RoomInfo: React.FC<RoomProps> = ({ room }) => {
} else {
//TODO: 자리 꽉찼다는 MODAL
}
}
}
, []);
return (
<button className={`flex items-center place-content-between m-2 w-5/6
...
...
web/src/contexts/SocketContext.ts
View file @
98a313f
import
React
from
'react'
;
import
{
io
}
from
'socket.io-client'
;
export
const
socket
=
io
(
'http://localhost/'
);
export
const
socket
=
io
(
'http://localhost
:3000
/'
);
const
SocketContext
=
React
.
createContext
(
socket
);
export
const
SocketProvider
=
SocketContext
.
Provider
;
...
...
web/src/pages/Login.tsx
View file @
98a313f
import React, { useContext, useState } from 'react';
import React, { useC
allback, useC
ontext, useState } from 'react';
import { useHistory } from 'react-router';
import { Main } from '../components/common/Main';
import { MessageResponse, MessageType } from '../components/common/types';
import { MessageResponse, MessageType
, RawMessage
} from '../components/common/types';
import SocketContext from '../contexts/SocketContext';
export const Login: React.FC = () => {
...
...
@@ -9,15 +9,19 @@ export const Login: React.FC = () => {
const socket = useContext(SocketContext);
const [ username, setUsername ] = useState("");
const login = () => {
socket.emit(MessageType.LOGIN, username, (response : MessageResponse<null>) => {
const login = useCallback(() => {
const rawMessage: RawMessage = {
type: MessageType.LOGIN,
message: { username }
}
socket.emit('msg', rawMessage, (response : MessageResponse<undefined>) => {
if (response.ok) {
history.push('/rooms');
} else {
console.error('login error!'); // TODO: 팝업 에러?
}
});
}
}
, []);
return (
<Main>
...
...
web/src/pages/Rooms.tsx
View file @
98a313f
import React, { useContext, useEffect, useState } from 'react';
import React, { useC
allback, useC
ontext, useEffect, useState } from 'react';
import { Main } from '../components/common/Main';
import { MessageResponse, MessageType } from '../components/common/types';
import { Room
Info } from '../components/rooms/RoomInfo
';
import { MessageResponse, MessageType
, RawMessage
} from '../components/common/types';
import { Room
Block } from '../components/rooms/RoomBlock
';
import { Room } from '../components/rooms/types';
import SocketContext from '../contexts/SocketContext';
...
...
@@ -9,41 +9,28 @@ export const Rooms: React.FC = () => {
const socket = useContext(SocketContext);
const [ rooms, setRooms ] = useState<Room[]>([]);
// for test
const testingRoom1: Room = {
uuid: '23525',
name: 'Hello World!',
currentUsers: 3,
maxUsers: 4
const refreshRooms = useCallback(() => {
const rawMessage: RawMessage = {
type: MessageType.ROOM_LIST_REQUEST,
message: {}
}
const testingRoom2: Room = {
uuid: '235252134',
name: 'Bonjour World!',
currentUsers: 6,
maxUsers: 6
}
useEffect(() => {
setRooms([testingRoom1, testingRoom2]);
// refreshRooms()
}, []);
const refreshRooms = () => {
socket.emit(MessageType.ROOM_LIST_REQUEST, (response: MessageResponse<Room[]>) => {
socket.emit('msg', rawMessage, (response: MessageResponse<Room[]>) => {
if (response.ok) {
setRooms(response.result!);
} else {
// TODO: 에러 핸들링
console.log("방 목록을 수신하지 못함");
console.log(response);
}
});
}
}
, []);
//
useEffect(refreshRooms, []);
useEffect(refreshRooms, []);
return (
<Main>
<div className='mt-auto w-screen flex flex-col items-center'>
{rooms.map((room) => (<Room
Info
key={room.uuid} room={room} />))}
{rooms.map((room) => (<Room
Block
key={room.uuid} room={room} />))}
</div>
</Main>
)
...
...
Please
register
or
login
to post a comment