import Echo from 'laravel-echo' ; import io from 'socket.io-client' ; import { getDvaApp } from 'umi' ; import { AuthModelState } from '@/models/connect' ; import { getToken , setSocketId } from '@/utils/authority' ; import { INotification } from '@/models/I' ; function createWebSocket ( ) { let echo ; return ( state ) => { if ( echo ) { setSocketId ( '' ) ; echo . disconnect ( ) ; } if ( ! state . logged ) { return ; } echo = new Echo ( { client : io , broadcaster : 'socket.io' , host : SOCKET_HOST , withoutInterceptors : true , auth : { headers : { Authorization : `Bearer ${ getToken ( ) }` , } , } , } ) ; setSocketId ( echo . socketId ( ) ) ; const app = getDvaApp ( ) ; echo . private ( `App.Models.User.${ state . user . id }` ) . listen ( 'UnreadNotificationsChange' , ( data ) => { app . _store . dispatch ( { type : 'auth/setUnreadCount' , unread_count : data . unread_count , } ) ; } ) . notification ( ( notification ) => { console . info ( notification ) ; } ) ; } ; } export default createWebSocket ( ) ;