Adding a Pusher container as a middleware/dispatcher between front and back

This commit is contained in:
David Négrier 2020-11-13 18:00:22 +01:00
parent 6c6d046891
commit 4c1e566a6c
86 changed files with 12172 additions and 983 deletions

View file

@ -95,7 +95,10 @@ class ConnectionManager {
console.log('An error occurred while connecting to socket server. Retrying');
reject(error);
});
// FIXME: onConnect should be triggered by the first JoinRoomEvent (instead of the connection)
connection.onConnect(() => {
console.warn('CONNECT RECEIVED');
resolve(connection);
})
}).catch((err) => {

View file

@ -4,6 +4,7 @@ import {SignalData} from "simple-peer";
import {BodyResourceDescriptionInterface} from "../Phaser/Entity/body_character";
export enum EventMessage{
CONNECT = "connect",
WEBRTC_SIGNAL = "webrtc-signal",
WEBRTC_SCREEN_SHARING_SIGNAL = "webrtc-screen-sharing-signal",
WEBRTC_START = "webrtc-start",
@ -121,8 +122,8 @@ export interface ItemEventMessageInterface {
}
export interface RoomJoinedMessageInterface {
users: MessageUserPositionInterface[],
groups: GroupCreatedUpdatedMessageInterface[],
//users: MessageUserPositionInterface[],
//groups: GroupCreatedUpdatedMessageInterface[],
items: { [itemId: number] : unknown }
}

View file

@ -93,6 +93,7 @@ export class RoomConnection implements RoomConnection {
};
this.socket.onmessage = (messageEvent) => {
console.warn('message received');
const arrayBuffer: ArrayBuffer = messageEvent.data;
const message = ServerToClientMessage.deserializeBinary(new Uint8Array(arrayBuffer));
@ -127,8 +128,8 @@ export class RoomConnection implements RoomConnection {
} else if (message.hasRoomjoinedmessage()) {
const roomJoinedMessage = message.getRoomjoinedmessage() as RoomJoinedMessage;
const users: Array<MessageUserJoined> = roomJoinedMessage.getUserList().map(this.toMessageUserJoined.bind(this));
const groups: Array<GroupCreatedUpdatedMessageInterface> = roomJoinedMessage.getGroupList().map(this.toGroupCreatedUpdatedMessage.bind(this));
//const users: Array<MessageUserJoined> = roomJoinedMessage.getUserList().map(this.toMessageUserJoined.bind(this));
//const groups: Array<GroupCreatedUpdatedMessageInterface> = roomJoinedMessage.getGroupList().map(this.toGroupCreatedUpdatedMessage.bind(this));
const items: { [itemId: number] : unknown } = {};
for (const item of roomJoinedMessage.getItemList()) {
items[item.getItemid()] = JSON.parse(item.getStatejson());
@ -137,9 +138,11 @@ export class RoomConnection implements RoomConnection {
this.userId = roomJoinedMessage.getCurrentuserid();
this.tags = roomJoinedMessage.getTagList();
this.dispatch(EventMessage.CONNECT, this);
this.dispatch(EventMessage.START_ROOM, {
users,
groups,
//users,
//groups,
items
});
} else if (message.hasErrormessage()) {
@ -199,7 +202,7 @@ export class RoomConnection implements RoomConnection {
const positionMessage = new PositionMessage();
positionMessage.setX(Math.floor(x));
positionMessage.setY(Math.floor(y));
let directionEnum: PositionMessage.DirectionMap[keyof PositionMessage.DirectionMap];
let directionEnum: Direction;
switch (direction) {
case 'up':
directionEnum = Direction.UP;
@ -355,8 +358,12 @@ export class RoomConnection implements RoomConnection {
this.socket.addEventListener('error', callback)
}
public onConnect(callback: (event: Event) => void): void {
/*public onConnect(callback: (e: Event) => void): void {
this.socket.addEventListener('open', callback)
}*/
public onConnect(callback: (roomConnection: RoomConnection) => void): void {
//this.socket.addEventListener('open', callback)
this.onMessage(EventMessage.CONNECT, callback);
}
/**

View file

@ -1,5 +1,5 @@
const DEBUG_MODE: boolean = process.env.DEBUG_MODE == "true";
const API_URL = (process.env.API_PROTOCOL || (typeof(window) !== 'undefined' ? window.location.protocol : 'http:')) + '//' + (process.env.API_URL || "api.workadventure.localhost");
const API_URL = (process.env.API_PROTOCOL || (typeof(window) !== 'undefined' ? window.location.protocol : 'http:')) + '//' + (process.env.API_URL || "pusher.workadventure.localhost");
const ADMIN_URL = API_URL.replace('api', 'admin');
const TURN_SERVER: string = process.env.TURN_SERVER || "turn:numb.viagenie.ca";
const TURN_USER: string = process.env.TURN_USER || 'g.parant@thecodingmachine.com';

View file

@ -56,7 +56,8 @@ import {Room} from "../../Connexion/Room";
import {jitsiFactory} from "../../WebRtc/JitsiFactory";
export interface GameSceneInitInterface {
initPosition: PointInterface|null
initPosition: PointInterface|null,
reconnecting: boolean
}
interface InitUserPositionEventInterface {
@ -140,6 +141,7 @@ export class GameScene extends ResizableScene implements CenterListener {
// The item that can be selected by pressing the space key.
private outlinedItem: ActionableItem|null = null;
private userInputManager!: UserInputManager;
private isReconnecting: boolean = false;
static createFromUrl(room: Room, mapUrlFile: string, gameSceneKey: string|null = null): GameScene {
// We use the map URL as a key
@ -299,6 +301,9 @@ export class GameScene extends ResizableScene implements CenterListener {
if (initData.initPosition !== undefined) {
this.initPosition = initData.initPosition;
}
if (initData.initPosition !== undefined) {
this.isReconnecting = initData.reconnecting;
}
}
//hook create scene
@ -426,7 +431,12 @@ export class GameScene extends ResizableScene implements CenterListener {
this.circleRedTexture.refresh();
// Let's pause the scene if the connection is not established yet
if (this.connection === undefined) {
if (this.isReconnecting) {
setTimeout(() => {
this.scene.sleep();
this.scene.launch(ReconnectingSceneName);
}, 0);
} else if (this.connection === undefined) {
// Let's wait 0.5 seconds before printing the "connecting" screen to avoid blinking
setTimeout(() => {
if (this.connection === undefined) {
@ -516,12 +526,15 @@ export class GameScene extends ResizableScene implements CenterListener {
//this.connection.emitPlayerDetailsMessage(gameManager.getPlayerName(), gameManager.getCharacterSelected())
connection.onStartRoom((roomJoinedMessage: RoomJoinedMessageInterface) => {
this.initUsersPosition(roomJoinedMessage.users);
//this.initUsersPosition(roomJoinedMessage.users);
this.connectionAnswerPromiseResolve(roomJoinedMessage);
// Analyze tags to find if we are admin. If yes, show console.
if (this.connection.hasTag('admin')) {
this.ConsoleGlobalMessageManager = new ConsoleGlobalMessageManager(this.connection, this.userInputManager);
}
this.scene.wake();
this.scene.sleep(ReconnectingSceneName);
});
connection.onUserJoins((message: MessageUserJoined) => {
@ -578,7 +591,8 @@ export class GameScene extends ResizableScene implements CenterListener {
initPosition: {
x: this.CurrentPlayer.x,
y: this.CurrentPlayer.y
}
},
reconnecting: true
});
this.scene.stop(this.scene.key);
@ -627,10 +641,6 @@ export class GameScene extends ResizableScene implements CenterListener {
this.gameMap.setPosition(event.x, event.y);
})
this.scene.wake();
this.scene.sleep(ReconnectingSceneName);
return connection;
});
}