Overnap

RawMessage 타입 추가하고 적용

......@@ -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_request",
ROOM_LIST_REQUEST: "roomList",
ROOM_JOIN: "room_join",
ROOM_LEAVE: "room_leave",
ROOM_USER_UPDATE: "room_user_update",
......
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 {
......
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);
}
});
}, []);
......