Teleport notification

- Create end point to permit the teleport notification. The map url will be /teleport/token_user
- Create message teleport
- Create receive feature in phaser to teleport

TODO
Teleport player on black room.
This commit is contained in:
Gregoire Parant 2020-10-13 11:39:07 +02:00
parent 3c2f134e4e
commit 65406f844e
8 changed files with 96 additions and 19 deletions

View File

@ -6,6 +6,7 @@ import {PrometheusController} from "./Controller/PrometheusController";
import {FileController} from "./Controller/FileController"; import {FileController} from "./Controller/FileController";
import {DebugController} from "./Controller/DebugController"; import {DebugController} from "./Controller/DebugController";
import {App as uwsApp} from "./Server/sifrr.server"; import {App as uwsApp} from "./Server/sifrr.server";
import {ReportController} from "./Controller/ReportController";
class App { class App {
public app: uwsApp; public app: uwsApp;
@ -15,6 +16,7 @@ class App {
public mapController: MapController; public mapController: MapController;
public prometheusController: PrometheusController; public prometheusController: PrometheusController;
private debugController: DebugController; private debugController: DebugController;
private reportController: ReportController;
constructor() { constructor() {
this.app = new uwsApp(); this.app = new uwsApp();
@ -24,6 +26,7 @@ class App {
this.authenticateController = new AuthenticateController(this.app); this.authenticateController = new AuthenticateController(this.app);
this.fileController = new FileController(this.app); this.fileController = new FileController(this.app);
this.mapController = new MapController(this.app); this.mapController = new MapController(this.app);
this.reportController = new ReportController(this.app, this.ioSocketController);
this.prometheusController = new PrometheusController(this.app, this.ioSocketController); this.prometheusController = new PrometheusController(this.app, this.ioSocketController);
this.debugController = new DebugController(this.app, this.ioSocketController); this.debugController = new DebugController(this.app, this.ioSocketController);
} }

View File

@ -1,4 +1,5 @@
import {HttpResponse} from "uWebSockets.js"; import {HttpRequest, HttpResponse} from "uWebSockets.js";
import {ADMIN_API_TOKEN} from "../Enum/EnvironmentVariable";
export class BaseController { export class BaseController {
@ -7,4 +8,11 @@ export class BaseController {
res.writeHeader('access-control-allow-methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); res.writeHeader('access-control-allow-methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.writeHeader('access-control-allow-origin', '*'); res.writeHeader('access-control-allow-origin', '*');
} }
protected checkAdminToken(req: HttpRequest): void {
//TODO
/*if(req.getHeader('Authorization') !== ADMIN_API_TOKEN){
throw 'Error token api';
}*/
}
} }

View File

@ -49,7 +49,7 @@ import {
SilentMessage, SilentMessage,
WebRtcSignalToClientMessage, WebRtcSignalToClientMessage,
WebRtcSignalToServerMessage, WebRtcSignalToServerMessage,
WebRtcStartMessage, WebRtcDisconnectMessage, PlayGlobalMessage, ReportPlayerMessage WebRtcStartMessage, WebRtcDisconnectMessage, PlayGlobalMessage, ReportPlayerMessage, TeleportMessageMessage
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import {UserMovesMessage} from "../Messages/generated/messages_pb"; import {UserMovesMessage} from "../Messages/generated/messages_pb";
import Direction = PositionMessage.Direction; import Direction = PositionMessage.Direction;
@ -108,22 +108,6 @@ export class IoSocketController {
return true; return true;
} }
/**
*
* @param token
*/
/* searchClientByToken(token: string): ExSocketInterface | null {
const clients: ExSocketInterface[] = Object.values(this.Io.sockets.sockets) as ExSocketInterface[];
for (let i = 0; i < clients.length; i++) {
const client = clients[i];
if (client.token !== token) {
continue
}
return client;
}
return null;
}*/
private async authenticate(req: HttpRequest): Promise<{ token: string, userUuid: string }> { private async authenticate(req: HttpRequest): Promise<{ token: string, userUuid: string }> {
//console.log(socket.handshake.query.token); //console.log(socket.handshake.query.token);
@ -921,4 +905,27 @@ export class IoSocketController {
public getWorlds(): Map<string, World> { public getWorlds(): Map<string, World> {
return this.Worlds; return this.Worlds;
} }
/**
*
* @param token
*/
searchClientByUuid(uuid: string): ExSocketInterface | null {
for(let socket of this.sockets.values()){
if(socket.userUuid === uuid){
return socket;
}
}
return null;
}
public teleport(userUuid: string) {
let user = this.searchClientByUuid(userUuid);
if(!user){
throw 'User not found';
}
const teleportMessageMessage = new TeleportMessageMessage();
teleportMessageMessage.setMap(`/teleport/${user.userUuid}`);
user.send(teleportMessageMessage.serializeBinary().buffer, true);
}
} }

View File

@ -0,0 +1,38 @@
import {BaseController} from "./BaseController";
import {HttpRequest, HttpResponse, TemplatedApp} from "uWebSockets.js";
import {IoSocketController} from "./IoSocketController";
export class ReportController extends BaseController {
constructor(private App: TemplatedApp, private ioSocketController: IoSocketController) {
super();
this.teleport();
}
teleport(){
this.App.options("/teleport", (res: HttpResponse, req: HttpRequest) => {
this.checkAdminToken(req);
this.addCorsHeaders(res);
res.end();
});
this.App.post("/teleport", (res: HttpResponse, req: HttpRequest) => {
(async () => {
try {
this.checkAdminToken(req);
this.addCorsHeaders(res);
res.onAborted(() => {
console.warn('Login request was aborted');
})
const param = await res.json();
this.ioSocketController.teleport(param.userUuid);
res.writeStatus("200 OK").end();
} catch (e) {
console.log("An error happened", e)
res.writeStatus(e.status || "500 Internal Server Error").end('An error happened');
}
})();
});
}
}

View File

@ -24,6 +24,8 @@ export enum EventMessage{
PLAY_GLOBAL_MESSAGE = "play-global-message", PLAY_GLOBAL_MESSAGE = "play-global-message",
STOP_GLOBAL_MESSAGE = "stop-global-message", STOP_GLOBAL_MESSAGE = "stop-global-message",
TELEPORT = "teleport",
} }
export interface PointInterface { export interface PointInterface {

View File

@ -22,7 +22,8 @@ import {
WebRtcSignalToClientMessage, WebRtcSignalToClientMessage,
WebRtcSignalToServerMessage, WebRtcSignalToServerMessage,
WebRtcStartMessage, WebRtcStartMessage,
ReportPlayerMessage ReportPlayerMessage,
TeleportMessageMessage
} from "../Messages/generated/messages_pb" } from "../Messages/generated/messages_pb"
import {UserSimplePeerInterface} from "../WebRtc/SimplePeer"; import {UserSimplePeerInterface} from "../WebRtc/SimplePeer";
@ -129,6 +130,8 @@ export class RoomConnection implements RoomConnection {
this.dispatch(EventMessage.PLAY_GLOBAL_MESSAGE, message.getPlayglobalmessage()); this.dispatch(EventMessage.PLAY_GLOBAL_MESSAGE, message.getPlayglobalmessage());
} else if (message.hasStopglobalmessage()) { } else if (message.hasStopglobalmessage()) {
this.dispatch(EventMessage.STOP_GLOBAL_MESSAGE, message.getStopglobalmessage()); this.dispatch(EventMessage.STOP_GLOBAL_MESSAGE, message.getStopglobalmessage());
} else if (message.hasTeleportmessagemessage()) {
this.dispatch(EventMessage.TELEPORT, message.getTeleportmessagemessage());
} else { } else {
throw new Error('Unknown message received'); throw new Error('Unknown message received');
} }
@ -466,6 +469,12 @@ export class RoomConnection implements RoomConnection {
}); });
} }
public receiveTeleportMessage(callback: (messageId: string) => void) {
return this.onMessage(EventMessage.TELEPORT, (message: TeleportMessageMessage) => {
callback(message.getMap());
});
}
public emitGlobalMessage(message: PlayGlobalMessageInterface){ public emitGlobalMessage(message: PlayGlobalMessageInterface){
console.log('emitGlobalMessage', message); console.log('emitGlobalMessage', message);
const playGlobalMessage = new PlayGlobalMessage(); const playGlobalMessage = new PlayGlobalMessage();

View File

@ -269,6 +269,11 @@ export class GameScene extends ResizableScene implements CenterListener {
item.fire(message.event, message.state, message.parameters); item.fire(message.event, message.state, message.parameters);
})); }));
connection.receiveTeleportMessage((map: string) => {
//TODO
console.log('receiveTeleportMessage', map);
})
// When connection is performed, let's connect SimplePeer // When connection is performed, let's connect SimplePeer
this.simplePeer = new SimplePeer(this.connection); this.simplePeer = new SimplePeer(this.connection);
this.GlobalMessageManager = new GlobalMessageManager(this.connection); this.GlobalMessageManager = new GlobalMessageManager(this.connection);

View File

@ -172,6 +172,10 @@ message WebRtcSignalToClientMessage {
string signal = 2; string signal = 2;
} }
message TeleportMessageMessage{
string map = 1;
}
message ServerToClientMessage { message ServerToClientMessage {
oneof message { oneof message {
BatchMessage batchMessage = 1; BatchMessage batchMessage = 1;
@ -184,5 +188,6 @@ message ServerToClientMessage {
WebRtcDisconnectMessage webRtcDisconnectMessage = 8; WebRtcDisconnectMessage webRtcDisconnectMessage = 8;
PlayGlobalMessage playGlobalMessage = 9; PlayGlobalMessage playGlobalMessage = 9;
StopGlobalMessage stopGlobalMessage = 10; StopGlobalMessage stopGlobalMessage = 10;
TeleportMessageMessage teleportMessageMessage = 11;
} }
} }