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

View File

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

View File

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

View File

@ -7,6 +7,7 @@ import {
GroupUpdateMessage,
ItemEventMessage,
PlayGlobalMessage,
UserGlobalMessage,
PositionMessage,
RoomJoinedMessage,
ServerToClientMessage,
@ -44,6 +45,7 @@ import {
MessageUserJoined,
OnConnectInterface,
PlayGlobalMessageInterface,
UserGlobalMessageInterface,
PositionInterface,
RoomJoinedMessageInterface,
ViewportInterface,
@ -643,4 +645,16 @@ export class RoomConnection implements RoomConnection {
public getAllTags(): string[] {
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;
QueryJitsiJwtMessage queryJitsiJwtMessage = 12;
EmotePromptMessage emotePromptMessage = 13;
UserGlobalMessage userGlobalMessage = 14;
}
}
@ -113,6 +114,12 @@ message PlayGlobalMessage {
string message = 3;
}
message UserGlobalMessage {
string type = 1;
string content = 2;
bool broadcastToWorld = 3;
}
message StopGlobalMessage {
string id = 1;
}
@ -260,6 +267,7 @@ message ServerToClientMessage {
RefreshRoomMessage refreshRoomMessage = 17;
WorldConnexionMessage worldConnexionMessage = 18;
EmoteEventMessage emoteEventMessage = 19;
UserGlobalMessage userGlobalMessage = 20;
}
}
@ -334,6 +342,7 @@ message PusherToBackMessage {
SendUserMessage sendUserMessage = 12;
BanUserMessage banUserMessage = 13;
EmotePromptMessage emotePromptMessage = 14;
UserGlobalMessage userGlobalMessage = 15;
}
}

View File

@ -17,6 +17,7 @@ import {
ServerToClientMessage,
CompanionMessage,
EmotePromptMessage,
UserGlobalMessage,
} from "../Messages/generated/messages_pb";
import { UserMovesMessage } from "../Messages/generated/messages_pb";
import { TemplatedApp } from "uWebSockets.js";
@ -381,6 +382,8 @@ export class IoSocketController {
client,
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 */

View File

@ -118,6 +118,18 @@ class AdminApi {
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();

View File

@ -1,42 +1,43 @@
import { PusherRoom } from "../Model/PusherRoom";
import { CharacterLayer, ExSocketInterface } from "../Model/Websocket/ExSocketInterface";
import {
AdminMessage,
AdminPusherToBackMessage,
AdminRoomMessage,
BanMessage,
CharacterLayerMessage,
EmoteEventMessage,
EmotePromptMessage,
GroupDeleteMessage,
ItemEventMessage,
JoinRoomMessage,
PlayGlobalMessage,
PusherToBackMessage,
QueryJitsiJwtMessage,
RefreshRoomMessage,
ReportPlayerMessage,
RoomJoinedMessage,
SendJitsiJwtMessage,
ServerToAdminClientMessage,
ServerToClientMessage,
SetPlayerDetailsMessage,
SilentMessage,
SubMessage,
ReportPlayerMessage,
UserGlobalMessage,
UserJoinedRoomMessage,
UserLeftMessage,
UserLeftRoomMessage,
UserMovesMessage,
ViewportMessage,
WebRtcSignalToServerMessage,
QueryJitsiJwtMessage,
SendJitsiJwtMessage,
JoinRoomMessage,
CharacterLayerMessage,
PusherToBackMessage,
WorldFullMessage,
WorldConnexionMessage,
AdminPusherToBackMessage,
ServerToAdminClientMessage,
EmoteEventMessage,
UserJoinedRoomMessage,
UserLeftRoomMessage,
AdminMessage,
BanMessage,
RefreshRoomMessage,
EmotePromptMessage,
WorldFullMessage,
} from "../Messages/generated/messages_pb";
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 { emitInBatch } from "./IoSocketHelpers";
import Jwt from "jsonwebtoken";
import { JITSI_URL } from "../Enum/EnvironmentVariable";
import { clientEventsEmitter } from "./ClientEventsEmitter";
import { gaugeManager } from "./GaugeManager";
import { apiClientRepository } from "./ApiClientRepository";
@ -605,6 +606,35 @@ export class SocketManager implements ZoneEventListener {
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();