import socketIO from 'socket.io' ; import { SocketListener } from '../lib/interface' ; import server from './server' ; const onlineSocketSet = new Set < socketIO . Socket > ( ) ; const eventListeners = { } ; server . use ( ( req ) => { if ( server . isLocalServer ( req ) && req . url . includes ( '/socket.io' ) ) return false ; return true ; } ) ; async function run ( ) { const io = socketIO ( await server . getHttpsServer ( ) ) ; io . on ( 'connection' , socket => { onlineSocketSet . add ( socket ) ; Object . entries ( eventListeners ) . forEach ( ( [ evtName , listener ] ) => { socket . on ( evtName , listener ) ; } ) ; socket . on ( 'disconnect' , ( ) => { onlineSocketSet . delete ( socket ) ; } ) ; } ) ; } function broadcast ( eventName , ... args ) { onlineSocketSet . forEach ( ( s ) => { s . emit ( eventName , ... args ) ; } ) ; } function once ( eventName ) { return Promise . race ( Array . from ( onlineSocketSet ) . map ( ( s ) => new Promise ( ( resolve ) => { function listener ( data ) { resolve ( data ) ; offEvtListener ( ) ; } function offEvtListener ( ) { Array . from ( onlineSocketSet ) . forEach ( ( s ) => { s . removeListener ( eventName , listener ) ; } ) ; } s . once ( eventName , listener ) ; } ) ) ) ; } function on ( evtName , cb ) { if ( eventListeners [ evtName ] ) return ; eventListeners [ evtName ] = cb ; Array . from ( onlineSocketSet ) . forEach ( ( socket ) => { socket . on ( evtName , cb ) ; } ) ; } export default { broadcast , on , once , run } ;