format import chalk from 'chalk' ; import { FlipperServerImpl } from 'flipper-server-core' ; import socketio from 'socket.io' ; export function startSocketServer ( flipperServer , socket ) { socket . on ( 'connection' , ( client ) => { console . log ( chalk . green ( `Client connected ${ client . id }` ) ) ; let connected = true ; function onServerEvent ( event , payoad ) { client . emit ( 'event' , event , payoad ) ; } flipperServer . onAny ( onServerEvent ) ; client . on ( 'exec' , ( id , command , args ) => { flipperServer . exec ( command , ... args ) . then ( ( result ) => { if ( connected ) { client . emit ( 'exec-response' , id , result ) ; } } ) . catch ( ( error ) => { if ( connected ) { client . emit ( 'exec-response-error' , id , error . toString ( ) + ( error . stack ? `\n${ error . stack }` : '' ) ) ; } } ) ; } ) ; client . on ( 'disconnect' , ( ) => { console . log ( chalk . red ( `Client disconnected ${ client . id }` ) ) ; connected = false ; flipperServer . offAny ( onServerEvent ) ; } ) ; client . on ( 'error' , ( e ) => { console . error ( chalk . red ( `Socket error ${ client . id }` ) , e ) ; connected = false ; flipperServer . offAny ( onServerEvent ) ; } ) ; } ) ; }