mirror of
https://github.com/yawaflua/LuckyDiamond.git
synced 2025-12-10 20:29:35 +02:00
Add chat logic class for web sockets
This commit is contained in:
@@ -1,15 +1,6 @@
|
||||
import { Post } from '../rest/RestMethods.js';
|
||||
import { BackendApiUrl } from '@/properties/Сonfig.js';
|
||||
|
||||
// export function
|
||||
// LogIn(authCode) {
|
||||
// return Post(BackendApiUrl + "/LogIn", { code: authCode }).then((data) => {
|
||||
// console.log(data);
|
||||
// return data;
|
||||
// }).catch(error => {
|
||||
// console.log(error)
|
||||
// });
|
||||
// }
|
||||
export async function LogIn(authCode) {
|
||||
try {
|
||||
const response = await Post(BackendApiUrl + "/Authorize/LogIn", { code: authCode });
|
||||
|
||||
50
luckydiamond/src/assets/js/chat/ChatLogic.js
Normal file
50
luckydiamond/src/assets/js/chat/ChatLogic.js
Normal file
@@ -0,0 +1,50 @@
|
||||
import { BackendWebSocketUrl } from '@/properties/Сonfig.js';
|
||||
import {
|
||||
GetCookie
|
||||
} from "@/assets/js/storage/CookieStorage.js";
|
||||
import WritechatComponent from "@/components/WritechatComponent.vue";
|
||||
|
||||
|
||||
let webSocket;
|
||||
|
||||
export function ConnectToChat() {
|
||||
try {
|
||||
webSocket = new WebSocket(BackendWebSocketUrl);
|
||||
|
||||
webSocket.onopen = function () {
|
||||
console.log('Connection established');
|
||||
// webSocket.send('Hello, Server!');
|
||||
};
|
||||
|
||||
webSocket.onmessage = function (event) {
|
||||
|
||||
WritechatComponent.ClaimDatamsg(event.data);
|
||||
console.log('Message from Server:', event.data);
|
||||
};
|
||||
|
||||
webSocket.onclose = function () {
|
||||
console.log('Connection closed');
|
||||
};
|
||||
|
||||
webSocket.onerror = function (event) {
|
||||
console.error('WebSocket Error:', event);
|
||||
};
|
||||
} catch (error) {
|
||||
console.error('Error in ConnectToChat:', error);
|
||||
}
|
||||
}
|
||||
|
||||
export function SendMessageToChat(message) {
|
||||
try {
|
||||
const data = {
|
||||
SpUserName: GetCookie("SpUserName"),
|
||||
SearchToken: GetCookie("SearchToken"),
|
||||
Message: message
|
||||
};
|
||||
|
||||
webSocket.send(JSON.stringify(data));
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error in ConnectToChat:', error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user