mirror of
https://github.com/yawaflua/LuckyDiamond.git
synced 2025-12-10 12:19:31 +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 { Post } from '../rest/RestMethods.js';
|
||||||
import { BackendApiUrl } from '@/properties/Сonfig.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) {
|
export async function LogIn(authCode) {
|
||||||
try {
|
try {
|
||||||
const response = await Post(BackendApiUrl + "/Authorize/LogIn", { code: authCode });
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,6 +34,7 @@
|
|||||||
<script>
|
<script>
|
||||||
import '@/assets/css/ComponentsStyles/chat.css'
|
import '@/assets/css/ComponentsStyles/chat.css'
|
||||||
import WritechatComponent from "@/components/WritechatComponent.vue";
|
import WritechatComponent from "@/components/WritechatComponent.vue";
|
||||||
|
import { SendMessageToChat } from "@/assets/js/chat/ChatLogic.js";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
components: { WritechatComponent },
|
components: { WritechatComponent },
|
||||||
@@ -54,6 +55,8 @@ export default {
|
|||||||
|
|
||||||
this.array.push(MsgUser)
|
this.array.push(MsgUser)
|
||||||
|
|
||||||
|
SendMessageToChat(msg[0]);
|
||||||
|
|
||||||
if(this.array.length > 7) {
|
if(this.array.length > 7) {
|
||||||
this.array.shift()
|
this.array.shift()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,11 +3,17 @@ import App from './App.vue'
|
|||||||
|
|
||||||
import router from "@/router/router";
|
import router from "@/router/router";
|
||||||
import { Mixins } from "@/mixins/mixin";
|
import { Mixins } from "@/mixins/mixin";
|
||||||
|
import {
|
||||||
|
ConnectToChat
|
||||||
|
} from "@/assets/js/chat/ChatLogic.js";
|
||||||
import '@/assets/css/global.css'
|
import '@/assets/css/global.css'
|
||||||
|
|
||||||
const app = createApp(App)
|
const app = createApp(App)
|
||||||
|
|
||||||
app.use(router)
|
app.use(router)
|
||||||
app.mixin(Mixins)
|
app.mixin(Mixins)
|
||||||
|
|
||||||
app.mount('#app')
|
app.mount('#app')
|
||||||
|
|
||||||
|
|
||||||
|
ConnectToChat();
|
||||||
@@ -1,2 +1,5 @@
|
|||||||
export const BackendApiUrl = 'https://spsystemcore20231122004605.azurewebsites.net/api';
|
export const BackendApiUrl = 'https://spsystemcore20231122004605.azurewebsites.net/api';
|
||||||
// export const BackendApiUrl = 'https://localhost:7062/api';
|
|
||||||
|
export const BackendWebSocketUrl = 'wss://spsystemcore20231122004605.azurewebsites.net';
|
||||||
|
|
||||||
|
// export const BackendApiUrl = 'https://localhost:7062/api';
|
||||||
Reference in New Issue
Block a user