workadventure/front/src/Connexion.ts

284 lines
7.5 KiB
TypeScript
Raw Normal View History

import {GameManager} from "./Phaser/Game/GameManager";
2020-04-07 20:41:35 +02:00
const SocketIo = require('socket.io-client');
import Axios from "axios";
import {API_URL, ROOM} from "./Enum/EnvironmentVariable";
2020-04-25 16:05:33 +02:00
enum EventMessage{
WEBRTC_SIGNAL = "webrtc-signal",
WEBRTC_START = "webrtc-start",
2020-05-02 20:46:02 +02:00
WEBRTC_JOIN_ROOM = "webrtc-join-room",
2020-04-25 16:05:33 +02:00
JOIN_ROOM = "join-room",
USER_POSITION = "user-position",
2020-05-02 20:46:02 +02:00
MESSAGE_ERROR = "message-error",
WEBRTC_DISCONNECT = "webrtc-disconect"
2020-04-25 16:05:33 +02:00
}
class Message {
userId: string;
roomId: string;
name: string;
character: string;
constructor(userId : string, roomId : string, name: string, character: string) {
this.userId = userId;
this.roomId = roomId;
this.name = name;
this.character = character;
}
toJson() {
return {
userId: this.userId,
roomId: this.roomId,
name: this.name,
character: this.character
}
}
}
export interface PointInterface {
x: number;
y: number;
direction : string;
toJson() : object;
}
class Point implements PointInterface{
x: number;
y: number;
2020-04-07 21:02:23 +02:00
direction : string;
2020-04-07 21:02:23 +02:00
constructor(x : number, y : number, direction : string = "none") {
if(x === null || y === null){
throw Error("position x and y cannot be null");
}
this.x = x;
this.y = y;
2020-04-07 21:02:23 +02:00
this.direction = direction;
}
toJson(){
return {
x : this.x,
2020-04-07 21:02:23 +02:00
y: this.y,
direction: this.direction
}
}
}
export interface MessageUserPositionInterface {
userId: string;
roomId: string;
name: string;
character: string;
position: PointInterface;
}
2020-04-25 16:05:33 +02:00
class MessageUserPosition extends Message implements MessageUserPositionInterface{
position: PointInterface;
constructor(userId : string, roomId : string, point : Point, name: string, character: string) {
super(userId, roomId, name, character);
this.position = point;
}
toString() {
return JSON.stringify(
Object.assign(
super.toJson(),
{
position: this.position.toJson()
})
);
}
}
export interface ListMessageUserPositionInterface {
2020-04-25 16:05:33 +02:00
roomId: string;
listUsersPosition: Array<MessageUserPosition>;
}
2020-04-25 16:05:33 +02:00
class ListMessageUserPosition {
roomId: string;
listUsersPosition: Array<MessageUserPosition>;
2020-04-25 16:05:33 +02:00
constructor(roomId: string, data: any) {
this.roomId = roomId;
this.listUsersPosition = new Array<MessageUserPosition>();
data.forEach((userPosition: any) => {
this.listUsersPosition.push(new MessageUserPosition(
userPosition.userId,
userPosition.roomId,
new Point(
userPosition.position.x,
userPosition.position.y,
userPosition.position.direction
),
userPosition.name,
userPosition.character
));
});
}
}
2020-04-25 16:05:33 +02:00
2020-04-12 13:57:00 +02:00
export interface ConnexionInterface {
2020-04-25 16:05:33 +02:00
socket: any;
token: string;
email: string;
2020-04-12 13:57:00 +02:00
userId: string;
2020-04-25 16:05:33 +02:00
startedRoom: string;
createConnexion(characterSelected: string): Promise<any>;
2020-04-25 16:05:33 +02:00
joinARoom(roomId: string, character: string): void;
2020-04-25 16:05:33 +02:00
sharePosition(x: number, y: number, direction: string, character: string): void;
2020-04-25 16:05:33 +02:00
positionOfAllUser(): void;
/*webrtc*/
sendWebrtcSignal(signal: any, roomId: string, userId?: string, receiverId?: string): void;
2020-04-25 16:05:33 +02:00
receiveWebrtcSignal(callBack: Function): void;
receiveWebrtcStart(callBack: Function): void;
2020-05-02 20:46:02 +02:00
disconnectMessage(callBack: Function): void;
2020-04-12 13:57:00 +02:00
}
2020-04-25 16:05:33 +02:00
export class Connexion implements ConnexionInterface {
socket: any;
token: string;
email: string;
userId: string;
2020-04-25 16:05:33 +02:00
startedRoom: string;
GameManager: GameManager;
2020-04-07 20:41:35 +02:00
constructor(email : string, GameManager: GameManager) {
this.email = email;
2020-04-07 20:41:35 +02:00
this.GameManager = GameManager;
}
createConnexion(characterSelected: string): Promise<ConnexionInterface> {
return Axios.post(`${API_URL}/login`, {email: this.email})
.then((res) => {
this.token = res.data.token;
this.startedRoom = res.data.roomId;
this.userId = res.data.userId;
this.socket = SocketIo(`${API_URL}`, {
query: {
token: this.token
}
});
//join the room
this.joinARoom(this.startedRoom, characterSelected);
//share your first position
this.sharePosition(0, 0, characterSelected);
this.positionOfAllUser();
this.errorMessage();
2020-04-12 13:57:00 +02:00
return this;
})
.catch((err) => {
console.error(err);
throw err;
});
}
/**
*
* @param roomId
* @param character
*/
joinARoom(roomId: string, character: string): void {
let messageUserPosition = new MessageUserPosition(
this.userId,
this.startedRoom,
new Point(0, 0),
this.email,
character
);
2020-04-25 16:05:33 +02:00
this.socket.emit(EventMessage.JOIN_ROOM, messageUserPosition.toString());
}
/**
2020-04-07 21:02:23 +02:00
*
* @param x
* @param y
* @param character
2020-04-07 21:02:23 +02:00
* @param direction
*/
sharePosition(x : number, y : number, character : string, direction : string = "none") : void{
2020-04-07 20:46:30 +02:00
if(!this.socket){
return;
}
let messageUserPosition = new MessageUserPosition(
this.userId,
ROOM[0],
new Point(x, y, direction),
this.email,
character
);
2020-04-25 16:05:33 +02:00
this.socket.emit(EventMessage.USER_POSITION, messageUserPosition.toString());
}
/**
* The data sent is an array with information for each user :
* [
* {
* userId: <string>,
* roomId: <string>,
* position: {
* x : <number>,
* y : <number>,
* direction: <string>
* }
* },
* ...
* ]
**/
2020-04-25 16:05:33 +02:00
positionOfAllUser(): void {
this.socket.on(EventMessage.USER_POSITION, (message: string) => {
let dataList = JSON.parse(message);
dataList.forEach((UserPositions: any) => {
let listMessageUserPosition = new ListMessageUserPosition(UserPositions[0], UserPositions[1]);
this.GameManager.shareUserPosition(listMessageUserPosition);
2020-04-07 20:41:35 +02:00
});
});
}
sendWebrtcSignal(signal: any, roomId: string, userId? : string, receiverId? : string) {
2020-05-02 20:46:02 +02:00
return this.socket.emit(EventMessage.WEBRTC_SIGNAL, JSON.stringify({
2020-04-26 17:43:21 +02:00
userId: userId ? userId : this.userId,
receiverId: receiverId ? receiverId : this.userId,
2020-04-25 16:05:33 +02:00
roomId: roomId,
signal: signal
}));
}
receiveWebrtcStart(callback: Function) {
this.socket.on(EventMessage.WEBRTC_START, callback);
}
receiveWebrtcSignal(callback: Function) {
2020-05-02 20:46:02 +02:00
return this.socket.on(EventMessage.WEBRTC_SIGNAL, callback);
2020-04-25 16:05:33 +02:00
}
errorMessage(): void {
this.socket.on(EventMessage.MESSAGE_ERROR, (message: string) => {
console.error(EventMessage.MESSAGE_ERROR, message);
})
}
2020-05-02 20:46:02 +02:00
disconnectMessage(callback: Function): void {
this.socket.on(EventMessage.WEBRTC_DISCONNECT, callback);
}
}