import io from 'socket.io-client' ; import { api } from '@config/index' ; import { setOnlineUserCount , sendNotification } from '@app/redux/actions/website' ; import { getAccessToken } from '@app/redux/reducers/user' ; import { loadTips } from '@app/redux/actions/tips' ; import { updateSession } from '@app/redux/actions/session' ; import { addMessagesToList } from '@app/redux/actions/message' ; let socket ; export const connect = function ( { dispatch , getState } ) { const me = getState ( ) . user . userInfo ; const accessToken = getAccessToken ( getState ( ) ) ; const handleActions = function ( action , params = null ) { action ( params ) ( dispatch , getState ) ; } ; const handleNotification = ( notification ) => { try { if ( typeof notification == 'string' ) { notification = JSON . parse ( notification ) ; } } catch ( err ) { notification = '' ; console . log ( err ) ; } if ( ! notification || ! notification . type ) return ; const { type , data } = notification ; switch ( type ) { case 'online-user' : handleActions ( setOnlineUserCount , data ) ; break ; case 'discover' : if ( ! me ) { setTimeout ( ( ) => { handleActions ( loadTips , 'discover' ) ; } , 1000 * 60 ) ; } else { handleActions ( loadTips , 'discover' ) ; } break ; case 'notification' : handleActions ( loadTips , 'notification' ) ; if ( data && data . comment_id && data . type == 'comment' || data && data . comment_id && data . type == 'reply' ) { let body = data . comment_id . content_html ; body = body . replace ( /<[^>]+>/g , '' ) ; body = body . replace ( /\r\n/g , '' ) ; body = body . replace ( /\n/g , '' ) ; handleActions ( sendNotification , { content : data . sender_id . nickname , option : { body , icon : 'https:' + data . sender_id . avatar_url , image : 'https:' + data . sender_id . avatar_url , tag : 'comment' , data : data } } ) ; } break ; case 'new-feed' : handleActions ( loadTips , 'new-feed' ) ; break ; case 'recommend-posts' : handleActions ( loadTips ) ; break ; case 'new-session' : handleActions ( loadTips , 'new-session' ) ; handleActions ( updateSession , data . sessionId ) ; handleActions ( addMessagesToList , data ) ; break ; } } ; socket = io ( api . socket , { reconnection : true , reconnectionAttempts : 15 , reconnectionDelay : 3000 , query : { accessToken } } ) ; socket . on ( "connect" , function ( ) { console . log ( 'socket connect success.' ) ; socket . on ( "all" , handleNotification ) ; if ( me ) socket . on ( me . _id , handleNotification ) ; if ( me ) socket . on ( 'member' , handleNotification ) ; } ) ; socket . on ( 'disconnect' , function ( ) { console . log ( 'socket has disconnect.' ) ; } ) ; if ( me && me . _id ) { handleActions ( loadTips ) ; } } ; export const close = ( ) => { if ( socket ) { socket . close ( ) ; socket = null ; } } ; export const addListener = ( target , callback ) => { socket . on ( target , callback ) ; } ; export const removeListener = ( target ) => { } ; export const emit = ( target , params , callback = ( ) => { } ) => { socket . emit ( target , params , callback ) ; } ;