2021-07-19 10:16:43 +02:00
|
|
|
import {
|
|
|
|
BatchMessage,
|
2021-07-19 15:57:50 +02:00
|
|
|
BatchToPusherMessage,
|
|
|
|
BatchToPusherRoomMessage,
|
2021-07-19 10:16:43 +02:00
|
|
|
ErrorMessage,
|
2021-07-19 15:57:50 +02:00
|
|
|
ServerToClientMessage,
|
|
|
|
SubToPusherMessage,
|
|
|
|
SubToPusherRoomMessage,
|
2021-07-19 10:16:43 +02:00
|
|
|
} from "../Messages/generated/messages_pb";
|
2021-06-24 10:09:10 +02:00
|
|
|
import { UserSocket } from "_Model/User";
|
2021-07-19 15:57:50 +02:00
|
|
|
import { RoomSocket, ZoneSocket } from "../RoomManager";
|
2020-12-04 15:58:31 +01:00
|
|
|
|
2021-12-23 15:27:17 +01:00
|
|
|
function getMessageFromError(error: unknown): string {
|
2021-12-23 14:26:49 +01:00
|
|
|
if (error instanceof Error) {
|
2021-12-23 15:27:17 +01:00
|
|
|
return error.message;
|
2021-12-23 14:26:49 +01:00
|
|
|
} else if (typeof error === "string") {
|
2021-12-23 15:27:17 +01:00
|
|
|
return error;
|
2021-12-23 14:26:49 +01:00
|
|
|
} else {
|
2021-12-23 15:27:17 +01:00
|
|
|
return "Unknown error";
|
2021-12-23 14:26:49 +01:00
|
|
|
}
|
2021-12-23 15:27:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export function emitError(Client: UserSocket, error: unknown): void {
|
|
|
|
const message = getMessageFromError(error);
|
2021-12-23 14:26:49 +01:00
|
|
|
|
2020-12-04 15:58:31 +01:00
|
|
|
const errorMessage = new ErrorMessage();
|
|
|
|
errorMessage.setMessage(message);
|
|
|
|
|
|
|
|
const serverToClientMessage = new ServerToClientMessage();
|
|
|
|
serverToClientMessage.setErrormessage(errorMessage);
|
|
|
|
|
|
|
|
//if (!Client.disconnecting) {
|
2021-06-24 10:09:10 +02:00
|
|
|
Client.write(serverToClientMessage);
|
2020-12-04 15:58:31 +01:00
|
|
|
//}
|
2021-11-24 16:20:07 +01:00
|
|
|
console.warn(message);
|
2020-12-04 15:58:31 +01:00
|
|
|
}
|
2021-07-19 10:16:43 +02:00
|
|
|
|
2021-12-23 15:27:17 +01:00
|
|
|
export function emitErrorOnRoomSocket(Client: RoomSocket, error: unknown): void {
|
|
|
|
console.error(error);
|
|
|
|
const message = getMessageFromError(error);
|
2021-07-19 10:16:43 +02:00
|
|
|
|
|
|
|
const errorMessage = new ErrorMessage();
|
|
|
|
errorMessage.setMessage(message);
|
|
|
|
|
|
|
|
const subToPusherRoomMessage = new SubToPusherRoomMessage();
|
|
|
|
subToPusherRoomMessage.setErrormessage(errorMessage);
|
|
|
|
|
|
|
|
const batchToPusherMessage = new BatchToPusherRoomMessage();
|
|
|
|
batchToPusherMessage.addPayload(subToPusherRoomMessage);
|
|
|
|
|
|
|
|
//if (!Client.disconnecting) {
|
|
|
|
Client.write(batchToPusherMessage);
|
|
|
|
//}
|
2021-11-24 16:20:07 +01:00
|
|
|
console.warn(message);
|
2021-07-19 10:16:43 +02:00
|
|
|
}
|
|
|
|
|
2021-12-23 15:27:17 +01:00
|
|
|
export function emitErrorOnZoneSocket(Client: ZoneSocket, error: unknown): void {
|
|
|
|
console.error(error);
|
|
|
|
const message = getMessageFromError(error);
|
2021-07-19 10:16:43 +02:00
|
|
|
|
|
|
|
const errorMessage = new ErrorMessage();
|
|
|
|
errorMessage.setMessage(message);
|
|
|
|
|
|
|
|
const subToPusherMessage = new SubToPusherMessage();
|
|
|
|
subToPusherMessage.setErrormessage(errorMessage);
|
|
|
|
|
|
|
|
const batchToPusherMessage = new BatchToPusherMessage();
|
|
|
|
batchToPusherMessage.addPayload(subToPusherMessage);
|
|
|
|
|
|
|
|
//if (!Client.disconnecting) {
|
|
|
|
Client.write(batchToPusherMessage);
|
|
|
|
//}
|
2021-11-24 16:20:07 +01:00
|
|
|
console.warn(message);
|
2021-07-19 10:16:43 +02:00
|
|
|
}
|