Create backend
- NodeJs - Express - Socket.io - Eslint - TypeScript
This commit is contained in:
parent
c0e75ae07e
commit
ba335aa33d
10 changed files with 2277 additions and 0 deletions
26
back/src/App.ts
Normal file
26
back/src/App.ts
Normal file
|
@ -0,0 +1,26 @@
|
|||
// lib/app.ts
|
||||
import {IoSocketController} from "./Controller/IoSocketController";
|
||||
import express from "express";
|
||||
import {Application} from 'express';
|
||||
import bodyParser = require('body-parser');
|
||||
import * as http from "http";
|
||||
|
||||
class App {
|
||||
public app: Application;
|
||||
public server: http.Server;
|
||||
public ioSocketController: IoSocketController;
|
||||
|
||||
constructor() {
|
||||
this.app = express();
|
||||
this.config();
|
||||
this.server = http.createServer(this.app);
|
||||
this.ioSocketController = new IoSocketController(this.server);
|
||||
}
|
||||
|
||||
private config(): void {
|
||||
this.app.use(bodyParser.json());
|
||||
this.app.use(bodyParser.urlencoded({extended: false}));
|
||||
}
|
||||
}
|
||||
|
||||
export default new App().server;
|
30
back/src/Controller/IoSocketController.ts
Normal file
30
back/src/Controller/IoSocketController.ts
Normal file
|
@ -0,0 +1,30 @@
|
|||
import socketIO = require('socket.io');
|
||||
import {Socket} from "socket.io";
|
||||
import * as http from "http";
|
||||
import {JoinRoomMessage} from "@Model/Websocket/JoinRoomMessage";
|
||||
|
||||
export class IoSocketController{
|
||||
Io: socketIO.Server;
|
||||
constructor(server : http.Server) {
|
||||
this.Io = socketIO(server);
|
||||
this.ioConnection();
|
||||
}
|
||||
|
||||
ioConnection() {
|
||||
this.Io.on('connection', (socket: Socket) => {
|
||||
//TODO check token access
|
||||
|
||||
/*join-rom event permit to join one room.
|
||||
message :
|
||||
userId : user identification
|
||||
roomId: room identification
|
||||
positionXUser: user x position map
|
||||
positionYUser: user y position on map
|
||||
*/
|
||||
socket.on('join-room', (message : JoinRoomMessage) => {
|
||||
socket.join(message.roomId);
|
||||
socket.to(message.roomId).emit('join-room', message.toString());
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
23
back/src/Model/Websocket/JoinRoomMessage.ts
Normal file
23
back/src/Model/Websocket/JoinRoomMessage.ts
Normal file
|
@ -0,0 +1,23 @@
|
|||
export class JoinRoomMessage {
|
||||
userId: string;
|
||||
roomId: string;
|
||||
positionXUser: string;
|
||||
positionYUser: string;
|
||||
|
||||
constructor(message: string) {
|
||||
let data = JSON.parse(message);
|
||||
this.userId = data.userId;
|
||||
this.roomId = data.roomId;
|
||||
this.positionXUser = data.positionXUser;
|
||||
this.positionYUser = data.positionYUser;
|
||||
}
|
||||
|
||||
toString(){
|
||||
return JSON.stringify({
|
||||
userId: this.userId,
|
||||
roomId: this.roomId,
|
||||
positionXUser: this.positionXUser,
|
||||
positionYUser: this.positionYUser
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue