Showing
9 changed files
with
104 additions
and
7 deletions
server/SocketHandler.ts
0 → 100644
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 | +} |
server/connection/Connection.ts
0 → 100644
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 | +} |
server/connection/ConnectionMapper.ts
0 → 100644
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}`)); | ... | ... |
server/message/handler/LoginHandler.ts
0 → 100644
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 | +} |
server/message/handler/MessageHandler.ts
0 → 100644
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 | +} |
server/message/types.ts
0 → 100644
-
Please register or login to post a comment