강동현

매우 간단한 로그인 로직 작성

1 +import { Socket } from "socket.io";
2 +import { ConnectionMapper } from "./connection/ConnectionMapper";
3 +
4 +export class SocketHandler {
5 + private connectionMapper: ConnectionMapper;
6 +
7 + constructor() {
8 + this.connectionMapper = new ConnectionMapper();
9 + }
10 +
11 + public connected(socket: Socket) {
12 + const connection = this.connectionMapper.get(socket);
13 + }
14 +}
1 +import { Socket } from "socket.io";
2 +import { MessageHandlerRegister } from "../message/handler/MessageHandlerRegister";
3 +import { User } from "../user/User";
4 +
5 +export class Connection {
6 + public readonly socket: Socket;
7 +
8 + public user?: User;
9 +
10 + constructor(socket: Socket) {
11 + this.socket = socket;
12 +
13 + MessageHandlerRegister.registerMessageHandlers(this);
14 + }
15 +
16 + public get authenticated(): boolean {
17 + return this.user !== undefined;
18 + }
19 +}
1 +import { Socket } from "socket.io";
2 +import { Connection } from "./Connection";
3 +
4 +export class ConnectionMapper {
5 + private map: Map<Socket, Connection>;
6 +
7 + constructor() {
8 + this.map = new Map<Socket, Connection>();
9 + }
10 +
11 + public get(socket: Socket): Connection {
12 + var value = this.map.get(socket);
13 + if (value) {
14 + return value;
15 + }
16 +
17 + value = new Connection(socket);
18 + return value;
19 + }
20 +
21 + public close(socket: Socket): void {
22 + this.map.delete(socket);
23 + }
24 +}
1 -import express from 'express'; 1 +import express from "express";
2 -import socketIo from 'socket.io'; 2 +import socketIo from "socket.io";
3 -import { createServer } from 'http'; 3 +import { createServer } from "http";
4 +import { SocketHandler } from "./SocketHandler";
4 5
5 const PORT = 3000; 6 const PORT = 3000;
6 7
...@@ -8,8 +9,10 @@ const app = express(); ...@@ -8,8 +9,10 @@ const app = express();
8 const server = createServer(app); 9 const server = createServer(app);
9 const io = new socketIo.Server(server); 10 const io = new socketIo.Server(server);
10 11
11 -io.on('connection', socket => { 12 +const handler = new SocketHandler();
12 - console.log('Connected.');
13 -})
14 13
15 -server.listen(PORT, () => console.log(`Listening on ${PORT}`)) 14 +io.on("connection", (socket) => {
15 + handler.connected(socket);
16 +});
17 +
18 +server.listen(PORT, () => console.log(`Listening on ${PORT}`));
......
1 +import { Connection } from "../../connection/Connection";
2 +import { User } from "../../user/User";
3 +import { LoginMessage } from "../types";
4 +import { MessageHandler } from "./MessageHandler";
5 +
6 +export class LoginHandler implements MessageHandler<LoginMessage> {
7 + public handle(connection: Connection, message: LoginMessage): void {
8 + connection.user = new User(message.username);
9 + console.log(`User ${message.username} has logged in!`);
10 + }
11 +}
1 +import { Connection } from "../../connection/Connection";
2 +
3 +export interface MessageHandler<T> {
4 + handle(connection: Connection, message: T): void;
5 +}
1 +import { Connection } from "../../connection/Connection";
2 +import { LoginMessage } from "../types";
3 +import { LoginHandler } from "./LoginHandler";
4 +
5 +export class MessageHandlerRegister {
6 + static registerMessageHandlers(connection: Connection) {
7 + connection.socket.on("login", (message: LoginMessage) => {
8 + new LoginHandler().handle(connection, message);
9 + });
10 + }
11 +}
1 +export interface LoginMessage {
2 + username: string;
3 +}
1 +export class User {
2 + public readonly username: string;
3 +
4 + constructor(username: string) {
5 + this.username = username;
6 + }
7 +}