Files
LuckyDiamond/luckydiamond/src/assets/js/chat/ChatLogic.js
2024-04-07 03:04:02 +02:00

90 lines
2.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { BackendWebSocketUrl } from '@/properties/Сonfig.js';
import { eventBus } from "@/main";
import {
GetCookie
} from "@/assets/js/storage/CookieStorage.js";
let webSocket;
export function ConnectToChat() {
try {
if (webSocket && webSocket.readyState === WebSocket.OPEN) {
return;
}
webSocket = new WebSocket(BackendWebSocketUrl);
webSocket.onopen = function () {
console.log('Connection established');
};
webSocket.onmessage = function (event) {
try {
const dataObject = JSON.parse(event.data);
if (dataObject && Object.prototype.hasOwnProperty.call(dataObject, 'SpUserName') && Object.prototype.hasOwnProperty.call(dataObject, 'Message')) {
eventBus.emit('dataChat', event.data);
return;
}
if (dataObject.MessageType == 'DoubleGameState') {
eventBus.emit('doubleGame', event.data)
return;
}
if (dataObject.MessageType == "CrashGameState") {
eventBus.emit('crash', event.data);
return;
}
if (Array.isArray(dataObject.CurrentGame.PlayerList)) {
eventBus.emit('jackpotGameTik', event.data);
return;
}
} catch (error) {
void (error);
}
};
webSocket.onclose = function () {
webSocket = new WebSocket(BackendWebSocketUrl);
console.log('Connection closed and reconnected');
};
webSocket.onerror = function (event) {
console.error('WebSocket Error:', event);
};
} catch (error) {
console.error('Error in ConnectToChat:', error);
}
}
export function SendMessageToChat(message) {
try {
if (!GetCookie("SpUserName") && !GetCookie("AUTHTOKEN") && !GetCookie("SearchToken")) {
return;
}
const userCredentials = {
SearchToken: GetCookie("SearchToken"),
AUTHTOKEN: GetCookie("AUTHTOKEN")
};
const messageObject = {
UserCredentials: userCredentials,
Message: message,
MessageType: "Chat"
};
webSocket.send(JSON.stringify(messageObject));
} catch (error) {
console.error('Error in ConnectToChat:', error);
}
}