base notification socket
This commit is contained in:
@@ -3,4 +3,4 @@ VITE_REFRESH_TOKEN_NAME = 'dmnu-a-rt'
|
|||||||
VITE_BASE_URL = 'https://dmenuplus-api.dev.danakcorp.com'
|
VITE_BASE_URL = 'https://dmenuplus-api.dev.danakcorp.com'
|
||||||
# VITE_BASE_URL = 'http://192.168.1.112:4001'
|
# VITE_BASE_URL = 'http://192.168.1.112:4001'
|
||||||
# VITE_SOCKET_URL = 'https://dmenuplus-api.dev.danakcorp.com/pager'
|
# VITE_SOCKET_URL = 'https://dmenuplus-api.dev.danakcorp.com/pager'
|
||||||
VITE_SOCKET_URL = 'http://192.168.99.242:4000/pager'
|
VITE_SOCKET_URL = 'http://192.168.99.242:4000'
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
import { getToken } from '@/config/func'
|
||||||
|
import { useSocket } from '@/pages/pager/hooks/useSocket'
|
||||||
|
import { type FC, useEffect, useCallback } from 'react'
|
||||||
|
|
||||||
|
const Notification: FC = () => {
|
||||||
|
|
||||||
|
// توکن بدون Bearer prefix (مثل کد HTML)
|
||||||
|
const token = getToken()
|
||||||
|
// اضافه کردن /notifications namespace به URL
|
||||||
|
const socketUrl = `${import.meta.env.VITE_SOCKET_URL}/notifications`
|
||||||
|
const { connect, socket, isConnected, error } = useSocket(socketUrl, token!)
|
||||||
|
|
||||||
|
// لاگ کردن وضعیت اتصال
|
||||||
|
useEffect(() => {
|
||||||
|
console.log('🔄 Notification: وضعیت اتصال:', isConnected ? 'متصل ✅' : 'قطع ❌')
|
||||||
|
}, [isConnected])
|
||||||
|
|
||||||
|
// لاگ کردن خطاها
|
||||||
|
useEffect(() => {
|
||||||
|
if (error) {
|
||||||
|
console.error('❌ Notification: خطا در اتصال:', error)
|
||||||
|
}
|
||||||
|
}, [error])
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
console.log('🔌 Notification: تلاش برای اتصال...', socketUrl)
|
||||||
|
connect()
|
||||||
|
}, [connect])
|
||||||
|
|
||||||
|
const handleGetNotification = useCallback((data: unknown) => {
|
||||||
|
console.log('✅ Notification: دریافت نوتیفیکیشنها:', data)
|
||||||
|
// alert('دریافت نوتیفیکیشنها: ' + JSON.stringify(data))
|
||||||
|
}, [])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isConnected) {
|
||||||
|
console.log('✅ Notification: متصل شد! ارسال درخواست...')
|
||||||
|
socket?.emit('get:notifications', { limit: 50 })
|
||||||
|
}
|
||||||
|
}, [isConnected, socket])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (socket) {
|
||||||
|
console.log('📡 Notification: تنظیم listener برای notifications:list')
|
||||||
|
|
||||||
|
const errorHandler = (error: unknown) => {
|
||||||
|
console.error('❌ Notification Error:', error)
|
||||||
|
}
|
||||||
|
|
||||||
|
const exceptionHandler = (error: unknown) => {
|
||||||
|
console.error('❌ Notification Exception:', error)
|
||||||
|
}
|
||||||
|
|
||||||
|
socket.on('notifications:list', handleGetNotification)
|
||||||
|
socket.on('error', errorHandler)
|
||||||
|
socket.on('exception', exceptionHandler)
|
||||||
|
|
||||||
|
// cleanup function
|
||||||
|
return () => {
|
||||||
|
socket.off('notifications:list', handleGetNotification)
|
||||||
|
socket.off('error', errorHandler)
|
||||||
|
socket.off('exception', exceptionHandler)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, [socket, handleGetNotification])
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div></div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Notification
|
||||||
@@ -74,6 +74,7 @@ export const useSocket = (
|
|||||||
reconnection: boolean;
|
reconnection: boolean;
|
||||||
reconnectionAttempts: number;
|
reconnectionAttempts: number;
|
||||||
reconnectionDelay: number;
|
reconnectionDelay: number;
|
||||||
|
transports: string[];
|
||||||
auth?: { token: string };
|
auth?: { token: string };
|
||||||
query?: Record<string, string>;
|
query?: Record<string, string>;
|
||||||
} = {
|
} = {
|
||||||
@@ -81,6 +82,7 @@ export const useSocket = (
|
|||||||
reconnection,
|
reconnection,
|
||||||
reconnectionAttempts,
|
reconnectionAttempts,
|
||||||
reconnectionDelay,
|
reconnectionDelay,
|
||||||
|
transports: ["websocket", "polling"], // مثل کد HTML
|
||||||
};
|
};
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ import ReviewsList from '@/pages/review/List'
|
|||||||
import DetailReview from '@/pages/review/Detail'
|
import DetailReview from '@/pages/review/Detail'
|
||||||
import PagersList from '@/pages/pager/List'
|
import PagersList from '@/pages/pager/List'
|
||||||
import NotificationsList from '@/pages/notifications/List'
|
import NotificationsList from '@/pages/notifications/List'
|
||||||
|
import Notification from '@/components/Notification'
|
||||||
const MainRouter: FC = () => {
|
const MainRouter: FC = () => {
|
||||||
|
|
||||||
const { hasSubMenu } = useSharedStore()
|
const { hasSubMenu } = useSharedStore()
|
||||||
@@ -103,6 +104,8 @@ const MainRouter: FC = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<Footer />
|
<Footer />
|
||||||
|
|
||||||
|
<Notification />
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user