SendGlobalMessage by sendAminMessage

This commit is contained in:
GRL 2021-07-20 15:16:51 +02:00
parent 0d3c697add
commit 6e65952d75
8 changed files with 101 additions and 28 deletions

View File

@ -9,7 +9,7 @@
export let game: Game; export let game: Game;
let inputSendTextActive = true; let inputSendTextActive = true;
let uploadMusicActive = false; let uploadMusicActive = false;
let handleSendText: { sendTextMessage(): void }; let handleSendText: { sendTextMessage(broadcast: boolean): void };
let handleSendAudio: { sendAudioMessage(): Promise<void> }; let handleSendAudio: { sendAudioMessage(): Promise<void> };
let broadcastToWorld = false; let broadcastToWorld = false;
@ -35,8 +35,7 @@
function send() { function send() {
if (inputSendTextActive) { if (inputSendTextActive) {
handleSendText.sendTextMessage(); handleSendText.sendTextMessage(broadcastToWorld);
console.log(typeof handleSendText);
} }
if (uploadMusicActive) { if (uploadMusicActive) {
handleSendAudio.sendAudioMessage(); handleSendAudio.sendAudioMessage();

View File

@ -3,7 +3,7 @@
import { onMount } from "svelte"; import { onMount } from "svelte";
import type { Game } from "../../Phaser/Game/Game"; import type { Game } from "../../Phaser/Game/Game";
import type { GameManager } from "../../Phaser/Game/GameManager"; import type { GameManager } from "../../Phaser/Game/GameManager";
import type { PlayGlobalMessageInterface } from "../../Connexion/ConnexionModels"; import type { UserGlobalMessageInterface } from "../../Connexion/ConnexionModels";
import { AdminMessageEventTypes } from "../../Connexion/AdminMessagesService"; import { AdminMessageEventTypes } from "../../Connexion/AdminMessagesService";
import type { Quill } from "quill"; import type { Quill } from "quill";
@ -41,20 +41,20 @@
const MESSAGE_TYPE = AdminMessageEventTypes.admin; const MESSAGE_TYPE = AdminMessageEventTypes.admin;
export const handleSending = { export const handleSending = {
sendTextMessage() { sendTextMessage(broadcastToWorld: boolean) {
if (gameScene == undefined) { if (gameScene == undefined) {
return; return;
} }
const text = quill.getText(0, quill.getLength()); const text = quill.getText(0, quill.getLength());
const GlobalMessage: PlayGlobalMessageInterface = { const textGlobalMessage: UserGlobalMessageInterface = {
id: "1", // FIXME: use another ID? type: MESSAGE_TYPE,
message: text, content: text,
type: MESSAGE_TYPE broadcastToWorld: broadcastToWorld
}; };
quill.deleteText(0, quill.getLength()); quill.deleteText(0, quill.getLength());
gameScene.connection?.emitGlobalMessage(GlobalMessage); gameScene.connection?.sendUserGlobalMessage(textGlobalMessage);
disableConsole(); disableConsole();
} }
} }

View File

@ -113,6 +113,12 @@ export interface PlayGlobalMessageInterface {
message: string; message: string;
} }
export interface UserGlobalMessageInterface {
type: string;
content: string;
broadcastToWorld: boolean;
}
export interface OnConnectInterface { export interface OnConnectInterface {
connection: RoomConnection; connection: RoomConnection;
room: RoomJoinedMessageInterface; room: RoomJoinedMessageInterface;

View File

@ -7,6 +7,7 @@ import {
GroupUpdateMessage, GroupUpdateMessage,
ItemEventMessage, ItemEventMessage,
PlayGlobalMessage, PlayGlobalMessage,
UserGlobalMessage,
PositionMessage, PositionMessage,
RoomJoinedMessage, RoomJoinedMessage,
ServerToClientMessage, ServerToClientMessage,
@ -44,6 +45,7 @@ import {
MessageUserJoined, MessageUserJoined,
OnConnectInterface, OnConnectInterface,
PlayGlobalMessageInterface, PlayGlobalMessageInterface,
UserGlobalMessageInterface,
PositionInterface, PositionInterface,
RoomJoinedMessageInterface, RoomJoinedMessageInterface,
ViewportInterface, ViewportInterface,
@ -643,4 +645,16 @@ export class RoomConnection implements RoomConnection {
public getAllTags(): string[] { public getAllTags(): string[] {
return this.tags; return this.tags;
} }
public sendUserGlobalMessage(message: UserGlobalMessageInterface): void {
const userGlobalMessage = new UserGlobalMessage();
userGlobalMessage.setType(message.type);
userGlobalMessage.setContent(message.content);
userGlobalMessage.setBroadcasttoworld(message.broadcastToWorld);
const clientToServerMessage = new ClientToServerMessage();
clientToServerMessage.setUserglobalmessage(userGlobalMessage);
this.socket.send(clientToServerMessage.serializeBinary().buffer);
}
} }

View File

@ -94,6 +94,7 @@ message ClientToServerMessage {
ReportPlayerMessage reportPlayerMessage = 11; ReportPlayerMessage reportPlayerMessage = 11;
QueryJitsiJwtMessage queryJitsiJwtMessage = 12; QueryJitsiJwtMessage queryJitsiJwtMessage = 12;
EmotePromptMessage emotePromptMessage = 13; EmotePromptMessage emotePromptMessage = 13;
UserGlobalMessage userGlobalMessage = 14;
} }
} }
@ -113,6 +114,12 @@ message PlayGlobalMessage {
string message = 3; string message = 3;
} }
message UserGlobalMessage {
string type = 1;
string content = 2;
bool broadcastToWorld = 3;
}
message StopGlobalMessage { message StopGlobalMessage {
string id = 1; string id = 1;
} }
@ -260,6 +267,7 @@ message ServerToClientMessage {
RefreshRoomMessage refreshRoomMessage = 17; RefreshRoomMessage refreshRoomMessage = 17;
WorldConnexionMessage worldConnexionMessage = 18; WorldConnexionMessage worldConnexionMessage = 18;
EmoteEventMessage emoteEventMessage = 19; EmoteEventMessage emoteEventMessage = 19;
UserGlobalMessage userGlobalMessage = 20;
} }
} }
@ -334,6 +342,7 @@ message PusherToBackMessage {
SendUserMessage sendUserMessage = 12; SendUserMessage sendUserMessage = 12;
BanUserMessage banUserMessage = 13; BanUserMessage banUserMessage = 13;
EmotePromptMessage emotePromptMessage = 14; EmotePromptMessage emotePromptMessage = 14;
UserGlobalMessage userGlobalMessage = 15;
} }
} }

View File

@ -17,6 +17,7 @@ import {
ServerToClientMessage, ServerToClientMessage,
CompanionMessage, CompanionMessage,
EmotePromptMessage, EmotePromptMessage,
UserGlobalMessage,
} 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 { TemplatedApp } from "uWebSockets.js"; import { TemplatedApp } from "uWebSockets.js";
@ -381,6 +382,8 @@ export class IoSocketController {
client, client,
message.getEmotepromptmessage() as EmotePromptMessage message.getEmotepromptmessage() as EmotePromptMessage
); );
} else if (message.hasUserglobalmessage()) {
socketManager.handleUserGlobalMessage(client, message.getUserglobalmessage() as UserGlobalMessage);
} }
/* Ok is false if backpressure was built up, wait for drain */ /* Ok is false if backpressure was built up, wait for drain */

View File

@ -118,6 +118,18 @@ class AdminApi {
return data.data; return data.data;
}); });
} }
async getUrlRoomsFromSameWorld(roomUrl: string): Promise<string[]> {
if (!ADMIN_API_URL) {
return Promise.reject(new Error("No admin backoffice set!"));
}
return Axios.get(ADMIN_API_URL + "/api/room/sameWorld" + "?roomUrl=" + encodeURIComponent(roomUrl), {
headers: { Authorization: `${ADMIN_API_TOKEN}` },
}).then((data) => {
return data.data;
});
}
} }
export const adminApi = new AdminApi(); export const adminApi = new AdminApi();

View File

@ -1,42 +1,43 @@
import { PusherRoom } from "../Model/PusherRoom"; import { PusherRoom } from "../Model/PusherRoom";
import { CharacterLayer, ExSocketInterface } from "../Model/Websocket/ExSocketInterface"; import { CharacterLayer, ExSocketInterface } from "../Model/Websocket/ExSocketInterface";
import { import {
AdminMessage,
AdminPusherToBackMessage,
AdminRoomMessage,
BanMessage,
CharacterLayerMessage,
EmoteEventMessage,
EmotePromptMessage,
GroupDeleteMessage, GroupDeleteMessage,
ItemEventMessage, ItemEventMessage,
JoinRoomMessage,
PlayGlobalMessage, PlayGlobalMessage,
PusherToBackMessage,
QueryJitsiJwtMessage,
RefreshRoomMessage,
ReportPlayerMessage,
RoomJoinedMessage, RoomJoinedMessage,
SendJitsiJwtMessage,
ServerToAdminClientMessage,
ServerToClientMessage, ServerToClientMessage,
SetPlayerDetailsMessage, SetPlayerDetailsMessage,
SilentMessage, SilentMessage,
SubMessage, SubMessage,
ReportPlayerMessage, UserGlobalMessage,
UserJoinedRoomMessage,
UserLeftMessage, UserLeftMessage,
UserLeftRoomMessage,
UserMovesMessage, UserMovesMessage,
ViewportMessage, ViewportMessage,
WebRtcSignalToServerMessage, WebRtcSignalToServerMessage,
QueryJitsiJwtMessage,
SendJitsiJwtMessage,
JoinRoomMessage,
CharacterLayerMessage,
PusherToBackMessage,
WorldFullMessage,
WorldConnexionMessage, WorldConnexionMessage,
AdminPusherToBackMessage, WorldFullMessage,
ServerToAdminClientMessage,
EmoteEventMessage,
UserJoinedRoomMessage,
UserLeftRoomMessage,
AdminMessage,
BanMessage,
RefreshRoomMessage,
EmotePromptMessage,
} from "../Messages/generated/messages_pb"; } from "../Messages/generated/messages_pb";
import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils"; import { ProtobufUtils } from "../Model/Websocket/ProtobufUtils";
import { ADMIN_API_URL, JITSI_ISS, SECRET_JITSI_KEY } from "../Enum/EnvironmentVariable"; import { ADMIN_API_URL, JITSI_ISS, JITSI_URL, SECRET_JITSI_KEY } from "../Enum/EnvironmentVariable";
import { adminApi } from "./AdminApi"; import { adminApi } from "./AdminApi";
import { emitInBatch } from "./IoSocketHelpers"; import { emitInBatch } from "./IoSocketHelpers";
import Jwt from "jsonwebtoken"; import Jwt from "jsonwebtoken";
import { JITSI_URL } from "../Enum/EnvironmentVariable";
import { clientEventsEmitter } from "./ClientEventsEmitter"; import { clientEventsEmitter } from "./ClientEventsEmitter";
import { gaugeManager } from "./GaugeManager"; import { gaugeManager } from "./GaugeManager";
import { apiClientRepository } from "./ApiClientRepository"; import { apiClientRepository } from "./ApiClientRepository";
@ -605,6 +606,35 @@ export class SocketManager implements ZoneEventListener {
client.backConnection.write(pusherToBackMessage); client.backConnection.write(pusherToBackMessage);
} }
public async handleUserGlobalMessage(
client: ExSocketInterface,
userGlobalMessageEvent: UserGlobalMessage
): Promise<void> {
if (!client.tags.includes("admin")) {
throw "Client is not an admin!";
}
const clientRoomUrl = client.roomId;
let tabUrlRooms: string[];
if (userGlobalMessageEvent.getBroadcasttoworld()) {
tabUrlRooms = await adminApi.getUrlRoomsFromSameWorld(clientRoomUrl);
} else {
tabUrlRooms = [clientRoomUrl];
}
let roomMessage = new AdminRoomMessage();
roomMessage.setMessage(userGlobalMessageEvent.getContent());
for (let roomUrl of tabUrlRooms) {
let apiRoom = await apiClientRepository.getClient(roomUrl);
roomMessage.setRoomid(roomUrl);
apiRoom.sendAdminMessageToRoom(roomMessage, (response) => {
return;
});
}
}
} }
export const socketManager = new SocketManager(); export const socketManager = new SocketManager();