Overnap

메세지 인터페이스 수정

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