2020-04-05 20:57:14 +02:00
|
|
|
import Axios from "axios";
|
2020-05-10 17:31:27 +02:00
|
|
|
import {API_URL} from "./Enum/EnvironmentVariable";
|
2020-05-13 20:22:42 +02:00
|
|
|
import {MessageUI} from "./Logger/MessageUI";
|
2020-05-15 22:40:06 +02:00
|
|
|
import {SetPlayerDetailsMessage} from "./Messages/SetPlayerDetailsMessage";
|
|
|
|
|
|
|
|
const SocketIo = require('socket.io-client');
|
|
|
|
import Socket = SocketIOClient.Socket;
|
2020-05-22 22:59:43 +02:00
|
|
|
import {PlayerAnimationNames} from "./Phaser/Player/Animation";
|
2020-06-08 09:20:36 +02:00
|
|
|
import {UserSimplePeerInterface} from "./WebRtc/SimplePeer";
|
2020-06-19 16:36:40 +02:00
|
|
|
import {SignalData} from "simple-peer";
|
2020-05-15 22:40:06 +02:00
|
|
|
|
2020-04-25 16:05:33 +02:00
|
|
|
enum EventMessage{
|
|
|
|
WEBRTC_SIGNAL = "webrtc-signal",
|
2020-06-11 23:18:06 +02:00
|
|
|
WEBRTC_SCREEN_SHARING_SIGNAL = "webrtc-screen-sharing-signal",
|
2020-04-25 16:05:33 +02:00
|
|
|
WEBRTC_START = "webrtc-start",
|
2020-05-19 19:11:12 +02:00
|
|
|
JOIN_ROOM = "join-room", // bi-directional
|
|
|
|
USER_POSITION = "user-position", // bi-directional
|
|
|
|
USER_MOVED = "user-moved", // From server to client
|
|
|
|
USER_LEFT = "user-left", // From server to client
|
2020-05-02 20:46:02 +02:00
|
|
|
MESSAGE_ERROR = "message-error",
|
2020-05-08 00:35:36 +02:00
|
|
|
WEBRTC_DISCONNECT = "webrtc-disconect",
|
|
|
|
GROUP_CREATE_UPDATE = "group-create-update",
|
|
|
|
GROUP_DELETE = "group-delete",
|
2020-06-17 15:37:02 +02:00
|
|
|
SET_PLAYER_DETAILS = "set-player-details", // Send the name and character to the server (on connect), receive back the id.
|
2020-05-13 20:22:42 +02:00
|
|
|
|
|
|
|
CONNECT_ERROR = "connect_error",
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface PointInterface {
|
|
|
|
x: number;
|
|
|
|
y: number;
|
|
|
|
direction : string;
|
2020-05-22 22:59:43 +02:00
|
|
|
moving: boolean;
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
export class Point implements PointInterface{
|
2020-05-22 22:59:43 +02:00
|
|
|
constructor(public x : number, public y : number, public direction : string = PlayerAnimationNames.WalkDown, public moving : boolean = false) {
|
2020-04-05 20:57:14 +02:00
|
|
|
if(x === null || y === null){
|
|
|
|
throw Error("position x and y cannot be null");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-10 12:54:05 +02:00
|
|
|
export interface MessageUserPositionInterface {
|
|
|
|
userId: string;
|
2020-05-03 22:24:14 +02:00
|
|
|
name: string;
|
2020-07-28 17:43:33 +02:00
|
|
|
characterLayers: string[];
|
2020-04-10 12:54:05 +02:00
|
|
|
position: PointInterface;
|
|
|
|
}
|
2020-04-25 16:05:33 +02:00
|
|
|
|
2020-05-19 19:11:12 +02:00
|
|
|
export interface MessageUserMovedInterface {
|
|
|
|
userId: string;
|
|
|
|
position: PointInterface;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface MessageUserJoined {
|
|
|
|
userId: string;
|
|
|
|
name: string;
|
2020-07-28 17:43:33 +02:00
|
|
|
characterLayers: string[];
|
2020-05-22 22:59:43 +02:00
|
|
|
position: PointInterface
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
2020-05-08 00:35:36 +02:00
|
|
|
export interface PositionInterface {
|
|
|
|
x: number,
|
|
|
|
y: number
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface GroupCreatedUpdatedMessageInterface {
|
|
|
|
position: PositionInterface,
|
|
|
|
groupId: string
|
|
|
|
}
|
|
|
|
|
2020-06-05 13:07:18 +02:00
|
|
|
export interface WebRtcStartMessageInterface {
|
|
|
|
roomId: string,
|
2020-06-08 09:20:36 +02:00
|
|
|
clients: UserSimplePeerInterface[]
|
2020-06-05 13:07:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface WebRtcDisconnectMessageInterface {
|
|
|
|
userId: string
|
|
|
|
}
|
|
|
|
|
2020-06-19 16:36:40 +02:00
|
|
|
export interface WebRtcSignalMessageInterface {
|
|
|
|
userId: string,
|
|
|
|
receiverId: string,
|
|
|
|
roomId: string,
|
|
|
|
signal: SignalData
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface StartMapInterface {
|
|
|
|
mapUrlStart: string,
|
|
|
|
startInstance: string
|
|
|
|
}
|
|
|
|
|
2020-06-22 11:58:07 +02:00
|
|
|
export class Connection implements Connection {
|
2020-06-22 18:42:54 +02:00
|
|
|
private readonly socket: Socket;
|
|
|
|
private userId: string|null = null;
|
2020-04-07 20:41:35 +02:00
|
|
|
|
2020-06-22 18:42:54 +02:00
|
|
|
private constructor(token: string) {
|
2020-06-22 15:00:23 +02:00
|
|
|
|
|
|
|
this.socket = SocketIo(`${API_URL}`, {
|
|
|
|
query: {
|
2020-06-22 18:42:54 +02:00
|
|
|
token: token
|
2020-06-22 15:00:23 +02:00
|
|
|
},
|
|
|
|
reconnection: false // Reconnection is handled by the application itself
|
|
|
|
});
|
|
|
|
|
|
|
|
this.socket.on(EventMessage.MESSAGE_ERROR, (message: string) => {
|
|
|
|
console.error(EventMessage.MESSAGE_ERROR, message);
|
|
|
|
})
|
2020-04-10 12:54:05 +02:00
|
|
|
}
|
|
|
|
|
2020-07-28 17:43:33 +02:00
|
|
|
public static createConnection(name: string, characterLayersSelected: string[]): Promise<Connection> {
|
2020-05-23 14:00:36 +02:00
|
|
|
return Axios.post(`${API_URL}/login`, {name: name})
|
2020-04-05 20:57:14 +02:00
|
|
|
.then((res) => {
|
2020-06-22 11:58:07 +02:00
|
|
|
|
2020-06-22 16:10:18 +02:00
|
|
|
return new Promise<Connection>((resolve, reject) => {
|
2020-06-22 18:42:54 +02:00
|
|
|
const connection = new Connection(res.data.token);
|
2020-06-22 16:10:18 +02:00
|
|
|
|
|
|
|
connection.onConnectError((error: object) => {
|
|
|
|
console.log('An error occurred while connecting to socket server. Retrying');
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
|
|
|
|
connection.socket.emit(EventMessage.SET_PLAYER_DETAILS, {
|
2020-06-22 18:42:54 +02:00
|
|
|
name: name,
|
2020-07-28 17:43:33 +02:00
|
|
|
characterLayers: characterLayersSelected
|
2020-06-22 16:10:18 +02:00
|
|
|
} as SetPlayerDetailsMessage, (id: string) => {
|
|
|
|
connection.userId = id;
|
|
|
|
});
|
|
|
|
|
|
|
|
resolve(connection);
|
|
|
|
});
|
2020-04-05 20:57:14 +02:00
|
|
|
})
|
|
|
|
.catch((err) => {
|
2020-06-22 15:00:23 +02:00
|
|
|
// Let's retry in 4-6 seconds
|
|
|
|
return new Promise<Connection>((resolve, reject) => {
|
|
|
|
setTimeout(() => {
|
2020-07-28 17:43:33 +02:00
|
|
|
Connection.createConnection(name, characterLayersSelected).then((connection) => resolve(connection))
|
2020-06-22 16:10:18 +02:00
|
|
|
.catch((error) => reject(error));
|
2020-06-22 15:00:23 +02:00
|
|
|
}, 4000 + Math.floor(Math.random() * 2000) );
|
|
|
|
});
|
2020-05-23 14:00:36 +02:00
|
|
|
});
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
2020-05-10 17:31:27 +02:00
|
|
|
|
2020-06-22 11:58:07 +02:00
|
|
|
public closeConnection(): void {
|
|
|
|
this.socket?.close();
|
2020-06-03 11:55:31 +02:00
|
|
|
}
|
|
|
|
|
2020-04-05 20:57:14 +02:00
|
|
|
|
2020-06-22 18:42:54 +02:00
|
|
|
public joinARoom(roomId: string, startX: number, startY: number, direction: string, moving: boolean): Promise<MessageUserPositionInterface[]> {
|
2020-06-22 16:11:48 +02:00
|
|
|
const promise = new Promise<MessageUserPositionInterface[]>((resolve, reject) => {
|
2020-06-22 15:00:23 +02:00
|
|
|
this.socket.emit(EventMessage.JOIN_ROOM, { roomId, position: {x: startX, y: startY, direction, moving }}, (userPositions: MessageUserPositionInterface[]) => {
|
2020-06-22 11:58:07 +02:00
|
|
|
resolve(userPositions);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
return promise;
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 18:42:54 +02:00
|
|
|
public sharePosition(x : number, y : number, direction : string, moving: boolean) : void{
|
2020-04-07 20:46:30 +02:00
|
|
|
if(!this.socket){
|
|
|
|
return;
|
|
|
|
}
|
2020-06-09 23:13:26 +02:00
|
|
|
const point = new Point(x, y, direction, moving);
|
2020-06-22 15:00:23 +02:00
|
|
|
this.socket.emit(EventMessage.USER_POSITION, point);
|
2020-04-05 20:57:14 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 11:58:07 +02:00
|
|
|
public onUserJoins(callback: (message: MessageUserJoined) => void): void {
|
2020-06-22 15:00:23 +02:00
|
|
|
this.socket.on(EventMessage.JOIN_ROOM, callback);
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 11:58:07 +02:00
|
|
|
public onUserMoved(callback: (message: MessageUserMovedInterface) => void): void {
|
2020-06-22 15:00:23 +02:00
|
|
|
this.socket.on(EventMessage.USER_MOVED, callback);
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 11:58:07 +02:00
|
|
|
public onUserLeft(callback: (userId: string) => void): void {
|
2020-06-22 15:00:23 +02:00
|
|
|
this.socket.on(EventMessage.USER_LEFT, callback);
|
2020-05-19 19:11:12 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 11:58:07 +02:00
|
|
|
public onGroupUpdatedOrCreated(callback: (groupCreateUpdateMessage: GroupCreatedUpdatedMessageInterface) => void): void {
|
2020-06-22 15:00:23 +02:00
|
|
|
this.socket.on(EventMessage.GROUP_CREATE_UPDATE, callback);
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 11:58:07 +02:00
|
|
|
public onGroupDeleted(callback: (groupId: string) => void): void {
|
2020-06-22 15:00:23 +02:00
|
|
|
this.socket.on(EventMessage.GROUP_DELETE, callback)
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 16:10:18 +02:00
|
|
|
public onConnectError(callback: (error: object) => void): void {
|
|
|
|
this.socket.on(EventMessage.CONNECT_ERROR, callback)
|
|
|
|
}
|
|
|
|
|
2020-06-22 18:42:54 +02:00
|
|
|
public sendWebrtcSignal(signal: unknown, roomId: string, userId? : string|null, receiverId? : string) {
|
2020-06-22 15:00:23 +02:00
|
|
|
return this.socket.emit(EventMessage.WEBRTC_SIGNAL, {
|
2020-04-26 17:43:21 +02:00
|
|
|
userId: userId ? userId : this.userId,
|
2020-04-26 19:12:01 +02:00
|
|
|
receiverId: receiverId ? receiverId : this.userId,
|
2020-04-25 16:05:33 +02:00
|
|
|
roomId: roomId,
|
|
|
|
signal: signal
|
2020-05-15 22:04:49 +02:00
|
|
|
});
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-14 14:47:16 +02:00
|
|
|
sendWebrtcScreenSharingSignal(signal: any, roomId: string, userId? : string|null) {
|
2020-06-11 23:18:06 +02:00
|
|
|
return this.getSocket().emit(EventMessage.WEBRTC_SCREEN_SHARING_SIGNAL, {
|
2020-06-14 14:47:16 +02:00
|
|
|
userId: userId,
|
2020-06-11 23:18:06 +02:00
|
|
|
roomId: roomId,
|
|
|
|
signal: signal
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-06-22 18:42:54 +02:00
|
|
|
public receiveWebrtcStart(callback: (message: WebRtcStartMessageInterface) => void) {
|
2020-06-22 15:00:23 +02:00
|
|
|
this.socket.on(EventMessage.WEBRTC_START, callback);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-22 18:42:54 +02:00
|
|
|
public receiveWebrtcSignal(callback: (message: WebRtcSignalMessageInterface) => void) {
|
2020-06-22 15:00:23 +02:00
|
|
|
return this.socket.on(EventMessage.WEBRTC_SIGNAL, callback);
|
2020-04-25 16:05:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 23:18:06 +02:00
|
|
|
receiveWebrtcScreenSharingSignal(callback: Function) {
|
|
|
|
return this.getSocket().on(EventMessage.WEBRTC_SCREEN_SHARING_SIGNAL, callback);
|
|
|
|
}
|
|
|
|
|
|
|
|
private errorMessage(): void {
|
|
|
|
this.getSocket().on(EventMessage.MESSAGE_ERROR, (message: string) => {
|
|
|
|
console.error(EventMessage.MESSAGE_ERROR, message);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private disconnectServer(): void {
|
|
|
|
this.getSocket().on(EventMessage.CONNECT_ERROR, () => {
|
|
|
|
this.GameManager.switchToDisconnectedScene();
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-06-22 15:00:23 +02:00
|
|
|
public onServerDisconnected(callback: (reason: string) => void): void {
|
|
|
|
this.socket.on('disconnect', (reason: string) => {
|
|
|
|
if (reason === 'io client disconnect') {
|
|
|
|
// The client asks for disconnect, let's not trigger any event.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback(reason);
|
2020-05-13 20:22:42 +02:00
|
|
|
});
|
|
|
|
|
2020-06-22 18:42:54 +02:00
|
|
|
}
|
2020-06-17 15:37:02 +02:00
|
|
|
|
2020-06-22 18:42:54 +02:00
|
|
|
public getUserId(): string|null {
|
|
|
|
return this.userId;
|
2020-05-13 20:22:42 +02:00
|
|
|
}
|
|
|
|
|
2020-06-05 13:07:18 +02:00
|
|
|
disconnectMessage(callback: (message: WebRtcDisconnectMessageInterface) => void): void {
|
2020-06-22 15:00:23 +02:00
|
|
|
this.socket.on(EventMessage.WEBRTC_DISCONNECT, callback);
|
2020-05-02 20:46:02 +02:00
|
|
|
}
|
2020-05-08 00:35:36 +02:00
|
|
|
}
|