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:34:13 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
7cf85a7e66493cd6a0ec37f9dfaf5ea343490e2a
7cf85a7e
1 parent
aa80dd22
RawMessage 타입 추가하고 적용
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
19 additions
and
7 deletions
web/src/components/common/types.ts
web/src/pages/Login.tsx
web/src/pages/Rooms.tsx
web/src/components/common/types.ts
View file @
7cf85a7
...
...
@@ -4,9 +4,14 @@ 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_LIST_REQUEST
:
"room
Li
st"
,
ROOM_JOIN
:
"room_join"
,
ROOM_LEAVE
:
"room_leave"
,
ROOM_USER_UPDATE
:
"room_user_update"
,
...
...
web/src/pages/Login.tsx
View file @
7cf85a7
import React, { useCallback, useContext, 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 = () => {
...
...
@@ -10,9 +10,11 @@ export const Login: React.FC = () => {
const [ username, setUsername ] = useState("");
const login = useCallback(() => {
console.log("emitted.");
socket.emit('msg', {type: MessageType.LOGIN, message: {username}}, (response : MessageResponse<undefined>) => {
console.log("responsed.");
const rawMessage: RawMessage = {
type: MessageType.LOGIN,
message: { username }
}
socket.emit('msg', rawMessage, (response : MessageResponse<undefined>) => {
if (response.ok) {
history.push('/rooms');
} else {
...
...
web/src/pages/Rooms.tsx
View file @
7cf85a7
import React, { useCallback, useContext, useEffect, useState } from 'react';
import { Main } from '../components/common/Main';
import { MessageResponse, MessageType } from '../components/common/types';
import { MessageResponse, MessageType
, RawMessage
} from '../components/common/types';
import { RoomBlock } from '../components/rooms/RoomBlock';
import { Room } from '../components/rooms/types';
import SocketContext from '../contexts/SocketContext';
...
...
@@ -10,12 +10,17 @@ export const Rooms: React.FC = () => {
const [ rooms, setRooms ] = useState<Room[]>([]);
const refreshRooms = useCallback(() => {
socket.emit(MessageType.ROOM_LIST_REQUEST, (response: MessageResponse<Room[]>) => {
const rawMessage: RawMessage = {
type: MessageType.ROOM_LIST_REQUEST,
message: {}
}
socket.emit('msg', rawMessage, (response: MessageResponse<Room[]>) => {
if (response.ok) {
setRooms(response.result!);
} else {
// TODO: 에러 핸들링
console.log("방 목록을 수신하지 못함");
console.log(response);
}
});
}, []);
...
...
Please
register
or
login
to post a comment