강동현

ts-transformer-keys 제거, 테스트 재작성

import { RoomDescription, RoomInfo } from "./dataType";
import { keys } from "ts-transformer-keys";
// 서버로 들어오는 메세지 타입을 정의합니다.
// 'result' 속성은 서버 요청 결과에만 포함되는 특별한 속성입니다.
......@@ -122,9 +121,12 @@ interface ServerOutboundMessageMap {
};
}
export type ServerInboundMessageKey = keyof ServerInboundMessageMap;
export interface RawMessage {
type: string;
message: any;
}
export const ServerInboundMessageKeyArray = keys<ServerInboundMessageMap>();
export type ServerInboundMessageKey = keyof ServerInboundMessageMap;
export type ServerInboundMessage<Key extends ServerInboundMessageKey> = Omit<
ServerInboundMessageMap[Key],
......@@ -143,5 +145,3 @@ export type ServerOutboundMessage<Key extends keyof ServerOutboundMessageMap> =
ServerOutboundMessageMap[Key];
export type ServerOutboundMessageKey = keyof ServerOutboundMessageMap;
export const ServerOutboundMessageKeyArray = keys<ServerOutboundMessageMap>();
......
......@@ -3,7 +3,5 @@
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"ts-transformer-keys": "^0.4.3"
}
"dependencies": {}
}
......
......@@ -2,7 +2,3 @@
# yarn lockfile v1
ts-transformer-keys@^0.4.3:
version "0.4.3"
resolved "https://registry.yarnpkg.com/ts-transformer-keys/-/ts-transformer-keys-0.4.3.tgz#d62389a40f430c00ef98fb9575fb6778a196e3ed"
integrity sha512-pOTLlet1SnAvhKNr9tMAFwuv5283OkUNiq1fXTEK+vrSv+kxU3e2Ijr/UkqyX2vuMmvcNHdpXC31hob7ljH//g==
......
......@@ -20,6 +20,9 @@ export class Connection {
type: T,
message: ServerOutboundMessage<T>
) {
this.socket.emit(type as string, message);
this.socket.emit("msg", {
type: type as string,
message: message,
});
}
}
......
import { Connection } from "../connection/Connection";
import {
RawMessage,
ServerInboundMessage,
ServerInboundMessageKey,
ServerOutboundMessageKeyArray,
ServerResponse,
} from "../../common/index";
import { User } from "../user/User";
......@@ -13,43 +13,52 @@ export class MessageHandlerChain {
constructor(connection: Connection) {
this.connection = connection;
this.connection.socket.on("msg", (raw: RawMessage, callback: Function) => {
this.handleRaw(connection, raw, callback);
});
}
private handleRaw(
connection: Connection,
raw: RawMessage,
callback: Function
) {
const type = raw.type as ServerInboundMessageKey;
const message = raw.message;
// 유저 정보가 없으므로 로그인은 따로 핸들링
this.connection.socket.on(
"login",
(message: ServerInboundMessage<"login">, callback: Function) => {
connection.user = new User(message.username, connection);
console.log(`User ${message.username} has logged in!`);
callback({ ok: true });
}
);
for (const key in ServerOutboundMessageKeyArray) {
const type = key as ServerInboundMessageKey;
this.connection.socket.on(key, (message: any, callback: Function) => {
// Game > Room > User 순으로 전달
if (
connection?.user?.room &&
connection.user.room.handler.handle(
type,
connection.user,
message,
callback
)
)
return;
if (
connection?.user &&
connection.user.handler.handle(
type,
connection.user,
message,
callback
)
)
return;
});
if (type === "login") {
this.handleLogin(connection, message, callback);
return;
}
// Game > Room > User 순으로 전달
if (
connection?.user?.room &&
connection.user.room.handler.handle(
type,
connection.user,
message,
callback
)
)
return;
if (
connection?.user &&
connection.user.handler.handle(type, connection.user, message, callback)
)
return;
}
private handleLogin(
connection: Connection,
message: ServerInboundMessage<"login">,
callback: Function
) {
connection.user = new User(message.username, connection);
console.log(`User ${message.username} has logged in!`);
callback({ ok: true });
}
}
......
import ioclient, { Socket } from "socket.io-client";
import {
LoginMessage,
MessageResponse,
MessageType,
RoomChatMessage,
RoomJoinMessage,
RoomLeaveMessage,
RoomListRequestMessage,
RoomUserUpdateMessage,
} from "./message/types";
import { expect } from "chai";
import { Server } from "./Server";
import { RoomDescription, RoomInfo } from "./room/types";
import { response } from "express";
import {
RawMessage,
ServerInboundMessage,
ServerInboundMessageKey,
ServerOutboundMessage,
ServerOutboundMessageKey,
ServerResponse,
} from "../common";
describe("server", () => {
const PORT = 3000;
......@@ -39,39 +36,58 @@ describe("server", () => {
client2.close();
});
var roomUserUpdateMessage: RoomUserUpdateMessage;
var roomChatMessage: RoomChatMessage;
var roomUserUpdateMessage: ServerOutboundMessage<"updateRoomUser">;
var roomChatMessage: ServerOutboundMessage<"chat">;
const send = <T extends ServerInboundMessageKey>(
socket: Socket,
type: T,
message: ServerInboundMessage<T>,
callback: (response: ServerResponse<T>) => void
) => {
socket.emit(
"msg",
{
type: type as string,
message: message,
},
callback
);
};
step("register listeners", () => {
client1.on(
MessageType.ROOM_USER_UPDATE,
(message: RoomUserUpdateMessage) => {
roomUserUpdateMessage = message;
}
);
client1.on("msg", (raw: RawMessage) => {
if (raw.type == "updateRoomUser") roomUserUpdateMessage = raw.message;
});
client1.on(MessageType.ROOM_CHAT, (message: RoomChatMessage) => {
roomChatMessage = message;
client1.on("msg", (raw: RawMessage) => {
if (raw.type == "chat") roomChatMessage = raw.message;
});
});
step("login 1", (done) => {
client1.emit(
MessageType.LOGIN,
new LoginMessage("guest1"),
(response: MessageResponse<undefined>) => {
expect(response.ok).to.equals(true);
send(
client1,
"login",
{
username: "guest1",
},
(response) => {
expect(response.ok).to.eq(true);
done();
}
);
});
step("login 2", (done) => {
client2.emit(
MessageType.LOGIN,
new LoginMessage("guest2"),
(response: MessageResponse<undefined>) => {
expect(response.ok).to.equals(true);
send(
client2,
"login",
{
username: "guest2",
},
(response) => {
expect(response.ok).to.eq(true);
done();
}
);
......@@ -80,52 +96,40 @@ describe("server", () => {
var roomToJoin: string;
step("room list", (done) => {
client1.emit(
MessageType.ROOM_LIST_REQUEST,
new RoomListRequestMessage(),
(response: MessageResponse<RoomDescription[]>) => {
expect(response.ok).to.eq(true);
expect(response.result !== undefined).to.eq(true);
if (response.result) {
expect(response.result[0].name).to.eq("테스트 방 #1");
roomToJoin = response.result[0].uuid;
}
done();
send(client1, "roomList", {}, (response) => {
expect(response.ok).to.eq(true);
expect(response.result !== undefined).to.eq(true);
if (response.result) {
expect(response.result[0].name).to.eq("테스트 방 #1");
roomToJoin = response.result[0].uuid;
}
);
done();
});
});
step("room join 1", (done) => {
client1.emit(
MessageType.ROOM_JOIN,
new RoomJoinMessage(roomToJoin),
(response: MessageResponse<RoomInfo>) => {
expect(response.ok).to.eq(true);
expect(response.result !== undefined).to.eq(true);
if (response.result) {
expect(response.result.uuid).to.eq(roomToJoin);
expect(response.result.users.length).to.eq(1);
expect(response.result.users[0].username).to.eq("guest1");
}
done();
send(client1, "joinRoom", { uuid: roomToJoin }, (response) => {
expect(response.ok).to.eq(true);
expect(response.result !== undefined).to.eq(true);
if (response.result) {
expect(response.result.uuid).to.eq(roomToJoin);
expect(response.result.users.length).to.eq(1);
expect(response.result.users[0].username).to.eq("guest1");
}
);
done();
});
});
step("room join 2", (done) => {
client2.emit(
MessageType.ROOM_JOIN,
new RoomJoinMessage(roomToJoin),
(response: MessageResponse<RoomInfo>) => {
expect(response.ok).to.eq(true);
expect(response.result !== undefined).to.eq(true);
if (response.result) {
expect(response.result.uuid).to.eq(roomToJoin);
expect(response.result.users.length).to.eq(2);
}
done();
send(client2, "joinRoom", { uuid: roomToJoin }, (response) => {
expect(response.ok).to.eq(true);
expect(response.result !== undefined).to.eq(true);
if (response.result) {
expect(response.result.uuid).to.eq(roomToJoin);
expect(response.result.users.length).to.eq(2);
}
);
done();
});
});
// TODO: RoomUserUpdateMessage가 아직 도착하지 않았는데 실행되는 경우
......@@ -133,19 +137,15 @@ describe("server", () => {
expect(roomUserUpdateMessage !== undefined).to.eq(true);
if (roomUserUpdateMessage) {
expect(roomUserUpdateMessage.state).to.eq("added");
expect(roomUserUpdateMessage.userdata.username).to.eq("guest2");
expect(roomUserUpdateMessage.user.username).to.eq("guest2");
}
});
step("client 2 send chat", (done) => {
client2.emit(
MessageType.ROOM_CHAT,
new RoomChatMessage("Hello World"),
(response: MessageResponse<undefined>) => {
expect(response.ok).to.eq(true);
done();
}
);
send(client2, "chat", { message: "Hello World" }, (response) => {
expect(response.ok).to.eq(true);
done();
});
});
step("client 1 received chat", () => {
......@@ -157,21 +157,17 @@ describe("server", () => {
});
step("client 2 leave", (done) => {
client2.emit(
MessageType.ROOM_LEAVE,
new RoomLeaveMessage(),
(response: MessageResponse<undefined>) => {
expect(response.ok).to.eq(true);
done();
}
);
send(client2, "leaveRoom", {}, (response) => {
expect(response.ok).to.eq(true);
done();
});
});
step("client 1 received user update", () => {
expect(roomUserUpdateMessage !== undefined).to.eq(true);
if (roomUserUpdateMessage) {
expect(roomUserUpdateMessage.state).to.eq("removed");
expect(roomUserUpdateMessage.userdata.username).to.eq("guest2");
expect(roomUserUpdateMessage.user.username).to.eq("guest2");
}
});
});
......