Overnap

메세지 인터페이스 수정

메세지 인터페이스 수정
1 -export interface MessageResponse<T> {
2 - ok: boolean;
3 - reason?: string;
4 - result?: T;
5 -}
6 -
7 -export class MessageType {
8 - static readonly LOGIN = "login";
9 - static readonly ROOM_LIST_REQUEST = "room_list_request";
10 - static readonly ROOM_JOIN = "room_join";
11 - static readonly ROOM_LEAVE = "room_leave";
12 - static readonly ROOM_USER_UPDATE = "room_user_update";
13 - static readonly ROOM_CHAT = "room_chat";
14 -}
1 +export interface MessageResponse<T> {
2 + ok: boolean;
3 + reason?: string;
4 + result?: T;
5 +}
6 +
7 +export const MessageType = {
8 + LOGIN: "login",
9 + ROOM_LIST_REQUEST: "room_list_request",
10 + ROOM_JOIN: "room_join",
11 + ROOM_LEAVE: "room_leave",
12 + ROOM_USER_UPDATE: "room_user_update",
13 + ROOM_CHAT: "room_chat",
14 +} as const
...\ No newline at end of file ...\ No newline at end of file
1 import React, { useContext, useState } from 'react'; 1 import React, { useContext, useState } from 'react';
2 import { RouteComponentProps } from 'react-router'; 2 import { RouteComponentProps } from 'react-router';
3 import { Footer } from '../components/common/Footer'; 3 import { Footer } from '../components/common/Footer';
4 -import type { MessageResponse } from '../components/common/types'; 4 +import { MessageResponse, MessageType } from '../components/common/types';
5 -import SocketContext from '../SocketContext'; 5 +import SocketContext from '../contexts/SocketContext';
6 6
7 export const Login: React.FC<RouteComponentProps> = ({ history }) => { 7 export const Login: React.FC<RouteComponentProps> = ({ history }) => {
8 const socket = useContext(SocketContext); 8 const socket = useContext(SocketContext);
9 const [ username, setUsername ] = useState(""); 9 const [ username, setUsername ] = useState("");
10 10
11 const login = () => { 11 const login = () => {
12 - socket.emit('login', username, (response : MessageResponse<null>) => { 12 + socket.emit(MessageType.LOGIN, username, (response : MessageResponse<null>) => {
13 if (response.ok) { 13 if (response.ok) {
14 history.push('/rooms'); 14 history.push('/rooms');
15 } else { 15 } else {
......
1 import React, { useContext, useEffect, useState } from 'react'; 1 import React, { useContext, useEffect, useState } from 'react';
2 import { RouteComponentProps } from 'react-router'; 2 import { RouteComponentProps } from 'react-router';
3 -import type { MessageResponse, MessageType } from '../components/common/types'; 3 +import { MessageResponse, MessageType } from '../components/common/types';
4 import SocketContext from '../contexts/SocketContext'; 4 import SocketContext from '../contexts/SocketContext';
5 5
6 interface Room { 6 interface Room {
...@@ -15,7 +15,7 @@ export const Rooms: React.FC<RouteComponentProps> = ({ history }) => { ...@@ -15,7 +15,7 @@ export const Rooms: React.FC<RouteComponentProps> = ({ history }) => {
15 const [ rooms, setRooms ] = useState<Room[]>([]); 15 const [ rooms, setRooms ] = useState<Room[]>([]);
16 16
17 const refreshRooms = () => { 17 const refreshRooms = () => {
18 - socket.emit("room_list_request", (response: MessageResponse<Room[]>) => { 18 + socket.emit(MessageType.ROOM_LIST_REQUEST, (response: MessageResponse<Room[]>) => {
19 if (response.ok) { 19 if (response.ok) {
20 setRooms(response.result!); 20 setRooms(response.result!);
21 } else { 21 } else {
......