Fix mediam stream manage and server back down

This commit is contained in:
Gregoire Parant 2020-05-23 14:00:36 +02:00
parent ab798b1c09
commit ab32021fc0
5 changed files with 61 additions and 20 deletions

View File

@ -15,21 +15,21 @@ export class AuthenticateController {
//permit to login on application. Return token to connect on Websocket IO. //permit to login on application. Return token to connect on Websocket IO.
login(){ login(){
// For now, let's completely forget the /login route. // For now, let's completely forget the /login route.
/*this.App.post("/login", (req: Request, res: Response) => { this.App.post("/login", (req: Request, res: Response) => {
let param = req.body; let param = req.body;
if(!param.email){ /*if(!param.name){
return res.status(BAD_REQUEST).send({ return res.status(BAD_REQUEST).send({
message: "email parameter is empty" message: "email parameter is empty"
}); });
} }*/
//TODO check user email for The Coding Machine game //TODO check user email for The Coding Machine game
let userId = uuid(); let userId = uuid();
let token = Jwt.sign({email: param.email, userId: userId}, SECRET_KEY, {expiresIn: '24h'}); let token = Jwt.sign({name: param.name, userId: userId}, SECRET_KEY, {expiresIn: '24h'});
return res.status(OK).send({ return res.status(OK).send({
token: token, token: token,
mapUrlStart: URL_ROOM_STARTED, mapUrlStart: URL_ROOM_STARTED,
userId: userId, userId: userId,
}); });
});*/ });
} }
} }

View File

@ -39,25 +39,42 @@ export class IoSocketController {
// Authentication with token. it will be decoded and stored in the socket. // Authentication with token. it will be decoded and stored in the socket.
// Completely commented for now, as we do not use the "/login" route at all. // Completely commented for now, as we do not use the "/login" route at all.
/*this.Io.use((socket: Socket, next) => { this.Io.use((socket: Socket, next) => {
if (!socket.handshake.query || !socket.handshake.query.token) { if (!socket.handshake.query || !socket.handshake.query.token) {
return next(new Error('Authentication error')); return next(new Error('Authentication error'));
} }
if(this.searchClientByToken(socket.handshake.query.token)){ if(this.searchClientByToken(socket.handshake.query.token)){
return next(new Error('Authentication error')); return next(new Error('Authentication error'));
} }
Jwt.verify(socket.handshake.query.token, SECRET_KEY, (err: JsonWebTokenError, tokenDecoded: object) => { Jwt.verify(socket.handshake.query.token, SECRET_KEY, (err: JsonWebTokenError, tokenDecoded: any) => {
if (err) { if (err) {
return next(new Error('Authentication error')); return next(new Error('Authentication error'));
} }
(socket as ExSocketInterface).token = tokenDecoded; (socket as ExSocketInterface).token = tokenDecoded;
(socket as ExSocketInterface).id = tokenDecoded.userId;
next(); next();
}); });
});*/ });
this.ioConnection(); this.ioConnection();
} }
/**
*
* @param token
*/
searchClientByToken(token: string): ExSocketInterface | null {
let clients: Array<any> = Object.values(this.Io.sockets.sockets);
for (let i = 0; i < clients.length; i++) {
let client: ExSocketInterface = clients[i];
if (client.token !== token) {
continue
}
return client;
}
return null;
}
private sendUpdateGroupEvent(group: Group): void { private sendUpdateGroupEvent(group: Group): void {
// Let's get the room of the group. To do this, let's get anyone in the group and find its room. // Let's get the room of the group. To do this, let's get anyone in the group and find its room.
// Note: this is suboptimal // Note: this is suboptimal

View File

@ -165,25 +165,20 @@ export class Connexion implements ConnexionInterface {
createConnexion(name: string, characterSelected: string): Promise<ConnexionInterface> { createConnexion(name: string, characterSelected: string): Promise<ConnexionInterface> {
this.name = name; this.name = name;
this.character = characterSelected; this.character = characterSelected;
/*return Axios.post(`${API_URL}/login`, {email: this.email}) return Axios.post(`${API_URL}/login`, {name: name})
.then((res) => { .then((res) => {
this.token = res.data.token; this.token = res.data.token;
this.userId = res.data.userId;*/
this.socket = SocketIo(`${API_URL}`, { this.socket = SocketIo(`${API_URL}`, {
/*query: { query: {
token: this.token token: this.token
}*/ }
}); });
return this.connectSocketServer(); return this.connectSocketServer();
/* return res.data;
}) })
.catch((err) => { .catch((err) => {
console.error(err); console.error(err);
throw err; throw err;
});*/ });
} }
/** /**

View File

@ -3,7 +3,7 @@ import {
GroupCreatedUpdatedMessageInterface, GroupCreatedUpdatedMessageInterface,
MessageUserJoined, MessageUserJoined,
MessageUserMovedInterface, MessageUserMovedInterface,
MessageUserPositionInterface MessageUserPositionInterface, PointInterface
} from "../../Connexion"; } from "../../Connexion";
import {CurrentGamerInterface, GamerInterface, hasMovedEventName, Player} from "../Player/Player"; import {CurrentGamerInterface, GamerInterface, hasMovedEventName, Player} from "../Player/Player";
import { DEBUG_MODE, RESOLUTION, ROOM, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable"; import { DEBUG_MODE, RESOLUTION, ROOM, ZOOM_LEVEL} from "../../Enum/EnvironmentVariable";
@ -14,6 +14,7 @@ import Sprite = Phaser.GameObjects.Sprite;
import CanvasTexture = Phaser.Textures.CanvasTexture; import CanvasTexture = Phaser.Textures.CanvasTexture;
import {AddPlayerInterface} from "./AddPlayerInterface"; import {AddPlayerInterface} from "./AddPlayerInterface";
import {PlayerAnimationNames} from "../Player/Animation"; import {PlayerAnimationNames} from "../Player/Animation";
import {MessageUserMoved} from "../../../../back/src/Model/Websocket/MessageUserMoved";
export enum Textures { export enum Textures {
Player = "male1" Player = "male1"
@ -410,6 +411,14 @@ export class GameScene extends Phaser.Scene {
* Create new player * Create new player
*/ */
public addPlayer(addPlayerData : AddPlayerInterface) : void{ public addPlayer(addPlayerData : AddPlayerInterface) : void{
//check if exist player, if exist, move position
if(this.MapPlayersByKey.has(addPlayerData.userId)){
this.updatePlayerPosition({
userId: addPlayerData.userId,
position: addPlayerData.position
});
return;
}
//initialise player //initialise player
let player = new Player( let player = new Player(
addPlayerData.userId, addPlayerData.userId,

View File

@ -141,9 +141,9 @@ export class SimplePeer implements SimplePeerInterface{
this.stream(user.userId, stream); this.stream(user.userId, stream);
}); });
this.PeerConnexionArray.get(user.userId).on('track', (track: MediaStreamTrack, stream: MediaStream) => { /*this.PeerConnexionArray.get(user.userId).on('track', (track: MediaStreamTrack, stream: MediaStream) => {
this.stream(user.userId, stream); this.stream(user.userId, stream);
}); });*/
this.PeerConnexionArray.get(user.userId).on('close', () => { this.PeerConnexionArray.get(user.userId).on('close', () => {
this.closeConnexion(user.userId); this.closeConnexion(user.userId);
@ -157,6 +157,13 @@ export class SimplePeer implements SimplePeerInterface{
console.info(`connect => ${user.userId}`); console.info(`connect => ${user.userId}`);
}); });
this.PeerConnexionArray.get(user.userId).on('data', (chunk: Buffer) => {
let data = JSON.parse(chunk.toString('utf8'));
if(data.type === "stream"){
this.stream(user.userId, data.stream);
}
});
this.addMedia(user.userId); this.addMedia(user.userId);
} }
@ -205,6 +212,11 @@ export class SimplePeer implements SimplePeerInterface{
* @param stream * @param stream
*/ */
private stream(userId : any, stream: MediaStream) { private stream(userId : any, stream: MediaStream) {
if(!stream){
this.MediaManager.disabledVideoByUserId(userId);
this.MediaManager.disabledMicrophoneByUserId(userId);
return;
}
this.MediaManager.addStreamRemoteVideo(userId, stream); this.MediaManager.addStreamRemoteVideo(userId, stream);
} }
@ -216,6 +228,14 @@ export class SimplePeer implements SimplePeerInterface{
try { try {
let transceiver : any = null; let transceiver : any = null;
if(!this.MediaManager.localStream){ if(!this.MediaManager.localStream){
//send fake signal
if(!this.PeerConnexionArray.has(userId)){
return;
}
this.PeerConnexionArray.get(userId).write(new Buffer(JSON.stringify({
type: "stream",
stream: null
})));
return; return;
} }
this.MediaManager.localStream.getTracks().forEach( this.MediaManager.localStream.getTracks().forEach(