Merge branch 'master' of https://github.com/thecodingmachine/workadventure into feature/back-players-proximity

This commit is contained in:
David MAECHLER 2020-04-06 00:47:20 +02:00
commit bb2315e7ac
12 changed files with 248 additions and 21 deletions

View file

@ -0,0 +1,28 @@
import {Application, Request, Response} from "express";
import Jwt, {JsonWebTokenError} from "jsonwebtoken";
import {BAD_REQUEST, OK} from "http-status-codes";
import {SECRET_KEY} from "../Enum/EnvironmentVariable";
export class AuthenticateController{
App : Application;
constructor(App : Application) {
this.App = App;
this.login();
}
//permit to login on application. Return token to connect on Websocket IO.
login(){
this.App.post("/login", (req: Request, res: Response) => {
let param = req.body;
if(!param.email){
return res.status(BAD_REQUEST).send({
message: "email parameter is empty"
});
}
//TODO check user email for The Coding Machine game
let token = Jwt.sign({email: param.email}, SECRET_KEY, {expiresIn: '24h'});
return res.status(OK).send({token: token});
});
}
}

View file

@ -1,36 +1,80 @@
import socketIO = require('socket.io');
import {Socket} from "socket.io";
import * as http from "http";
import {MessageUserPosition} from "@Model/Websocket/MessageUserPosition";
import {MessageUserPosition} from "../Model/Websocket/MessageUserPosition"; //TODO fix import by "_Model/.."
import {ExSocketInterface} from "../Model/Websocket/ExSocketInterface"; //TODO fix import by "_Model/.."
import Jwt, {JsonWebTokenError} from "jsonwebtoken";
import {SECRET_KEY} from "../Enum/EnvironmentVariable"; //TODO fix import by "_Enum/..."
export class IoSocketController{
Io: socketIO.Server;
constructor(server : http.Server) {
this.Io = socketIO(server);
// Authentication with token. it will be decoded and stored in the socket.
this.Io.use( (socket: Socket, next) => {
if (!socket.handshake.query || !socket.handshake.query.token) {
return next(new Error('Authentication error'));
}
Jwt.verify(socket.handshake.query.token, SECRET_KEY, (err: JsonWebTokenError, tokenDecoded: object) => {
if (err) {
return next(new Error('Authentication error'));
}
(socket as ExSocketInterface).token = tokenDecoded;
next();
});
});
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
position: position of user in map
x: user x position on map
y: user y position on map
*/
socket.on('join-room', (message : MessageUserPosition) => {
socket.join(message.roomId);
socket.on('join-room', (message : string) => {
let messageUserPosition = this.hydrateMessageReceive(message);
if(messageUserPosition instanceof Error){
return socket.emit("message-error", JSON.stringify({message: messageUserPosition.message}))
}
//join user in room
socket.join(messageUserPosition.roomId);
// sending to all clients in room except sender
socket.to(message.roomId).emit('join-room', message.toString());
this.saveUserPosition((socket as ExSocketInterface), messageUserPosition);
socket.to(messageUserPosition.roomId).emit('join-room', messageUserPosition.toString());
});
socket.on('user-position', (message : MessageUserPosition) => {
socket.on('user-position', (message : string) => {
let messageUserPosition = this.hydrateMessageReceive(message);
if(messageUserPosition instanceof Error){
return socket.emit("message-error", JSON.stringify({message: messageUserPosition.message}));
}
// sending to all clients in room except sender
socket.to(message.roomId).emit('join-room', message.toString());
this.saveUserPosition((socket as ExSocketInterface), messageUserPosition);
socket.to(messageUserPosition.roomId).emit('join-room', messageUserPosition.toString());
});
});
}
}
//permit to save user position in socket
saveUserPosition(socket : ExSocketInterface, message : MessageUserPosition){
socket.position = message.position;
}
//Hydrate and manage error
hydrateMessageReceive(message : string) : MessageUserPosition | Error{
try {
return new MessageUserPosition(message);
}catch (err) {
//TODO log error
return new Error(err);
}
}
}