mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
add websockets support22
This commit is contained in:
@@ -45,6 +45,9 @@ async def create_request(
|
||||
# Отправляем уведомление в Telegram
|
||||
await notify_new_request(db_request.id)
|
||||
|
||||
# Получаем актуальную статистику
|
||||
stats = requests.get_statistics(db)
|
||||
|
||||
# Получаем полные данные о заявке для отправки через WebSocket
|
||||
request_data = {
|
||||
"id": db_request.id,
|
||||
@@ -61,7 +64,11 @@ async def create_request(
|
||||
# Отправляем уведомление через WebSocket всем админам
|
||||
await notification_manager.broadcast_to_admins({
|
||||
"type": "new_request",
|
||||
"data": request_data
|
||||
"data": request_data,
|
||||
"statistics": {
|
||||
"total": stats["total"],
|
||||
"by_status": stats["by_status"]
|
||||
}
|
||||
})
|
||||
return db_request
|
||||
|
||||
@@ -96,12 +103,19 @@ async def update_request_status(
|
||||
if db_request is None:
|
||||
raise HTTPException(status_code=404, detail="Request not found")
|
||||
|
||||
# Получаем актуальную статистику
|
||||
stats = requests.get_statistics(db)
|
||||
|
||||
# Отправляем уведомление через WebSocket
|
||||
await notification_manager.broadcast_to_admins({
|
||||
"type": "status_update",
|
||||
"data": {
|
||||
"id": request_id,
|
||||
"status": db_request.status
|
||||
},
|
||||
"statistics": {
|
||||
"total": stats["total"],
|
||||
"by_status": stats["by_status"]
|
||||
}
|
||||
})
|
||||
return db_request
|
||||
|
@@ -1,6 +1,9 @@
|
||||
from fastapi import WebSocket
|
||||
from typing import Dict, List
|
||||
import json
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class NotificationManager:
|
||||
def __init__(self):
|
||||
@@ -12,25 +15,47 @@ class NotificationManager:
|
||||
async def connect(self, websocket: WebSocket, client_type: str):
|
||||
await websocket.accept()
|
||||
self.active_connections[client_type].append(websocket)
|
||||
logger.info(f"New {client_type} connection. Total connections: {len(self.active_connections[client_type])}")
|
||||
|
||||
def disconnect(self, websocket: WebSocket, client_type: str):
|
||||
self.active_connections[client_type].remove(websocket)
|
||||
if websocket in self.active_connections[client_type]:
|
||||
self.active_connections[client_type].remove(websocket)
|
||||
logger.info(f"{client_type} disconnected. Remaining connections: {len(self.active_connections[client_type])}")
|
||||
|
||||
async def broadcast_to_admins(self, message: dict):
|
||||
"""Отправка сообщения всем подключенным админам"""
|
||||
logger.info(f"Broadcasting to admins: {message}")
|
||||
disconnected = []
|
||||
|
||||
for connection in self.active_connections["admin"]:
|
||||
try:
|
||||
await connection.send_json(message)
|
||||
except:
|
||||
# Если не удалось отправить сообщение, пропускаем
|
||||
logger.info("Message sent successfully")
|
||||
except Exception as e:
|
||||
logger.error(f"Error sending message: {e}")
|
||||
disconnected.append(connection)
|
||||
continue
|
||||
|
||||
# Удаляем отключенные соединения
|
||||
for connection in disconnected:
|
||||
self.disconnect(connection, "admin")
|
||||
|
||||
async def broadcast_to_employees(self, employee_id: int, message: dict):
|
||||
"""Отправка сообщения конкретному сотруднику"""
|
||||
logger.info(f"Broadcasting to employee {employee_id}: {message}")
|
||||
disconnected = []
|
||||
|
||||
for connection in self.active_connections["employee"]:
|
||||
try:
|
||||
await connection.send_json(message)
|
||||
except:
|
||||
logger.info("Message sent successfully")
|
||||
except Exception as e:
|
||||
logger.error(f"Error sending message: {e}")
|
||||
disconnected.append(connection)
|
||||
continue
|
||||
|
||||
# Удаляем отключенные соединения
|
||||
for connection in disconnected:
|
||||
self.disconnect(connection, "employee")
|
||||
|
||||
notification_manager = NotificationManager()
|
@@ -72,7 +72,7 @@
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, onUnmounted } from 'vue'
|
||||
import { ref, onMounted, onUnmounted, watch } from 'vue'
|
||||
import axios from '@/plugins/axios'
|
||||
import { wsClient } from '@/plugins/websocket'
|
||||
import { formatDate } from '@/utils/date'
|
||||
@@ -101,32 +101,21 @@ const fetchData = async () => {
|
||||
const handleWebSocketMessage = async (data: any) => {
|
||||
console.log('Received WebSocket message:', data)
|
||||
|
||||
// Обновляем статистику, если она пришла в сообщении
|
||||
if (data.statistics) {
|
||||
statistics.value = data.statistics
|
||||
}
|
||||
|
||||
if (data.type === 'new_request') {
|
||||
try {
|
||||
// Получаем актуальную статистику
|
||||
const statsResponse = await axios.get('/api/requests/statistics')
|
||||
statistics.value = statsResponse.data
|
||||
|
||||
// Добавляем новую заявку в начало списка
|
||||
if (data.data) {
|
||||
requests.value = [data.data, ...requests.value]
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error updating data:', error)
|
||||
// Добавляем новую заявку в начало списка
|
||||
if (data.data) {
|
||||
requests.value = [data.data, ...requests.value]
|
||||
}
|
||||
} else if (data.type === 'status_update') {
|
||||
try {
|
||||
// Получаем актуальную статистику
|
||||
const statsResponse = await axios.get('/api/requests/statistics')
|
||||
statistics.value = statsResponse.data
|
||||
|
||||
// Обновляем статус заявки в списке
|
||||
const request = requests.value.find(r => r.id === data.data.id)
|
||||
if (request) {
|
||||
request.status = data.data.status
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error updating status:', error)
|
||||
// Обновляем статус заявки в списке
|
||||
const request = requests.value.find(r => r.id === data.data.id)
|
||||
if (request) {
|
||||
request.status = data.data.status
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -141,6 +130,15 @@ onMounted(() => {
|
||||
}, 1000)
|
||||
})
|
||||
|
||||
// Переподключение WebSocket при потере соединения
|
||||
watch(() => wsClient.isConnected, (isConnected) => {
|
||||
if (!isConnected) {
|
||||
setTimeout(() => {
|
||||
wsClient.connect('admin')
|
||||
}, 3000)
|
||||
}
|
||||
})
|
||||
|
||||
// Отключение от WebSocket при размонтировании компонента
|
||||
onUnmounted(() => {
|
||||
wsClient.removeMessageHandler(handleWebSocketMessage)
|
||||
|
Reference in New Issue
Block a user