import * as express from "express" ; import * as settings from "electron-settings" ; import * as socketIo from "socket.io" ; import { NativeImage } from "electron" ; export class Connection { public socket : any ; private exp : any ; private http : any ; private io : any ; public startServer = ( callback ) => { if ( settings . get ( "appServer" ) as boolean ) { try { const port = settings . get ( "appPort" ) ; this . exp = express ( ) ; this . exp . set ( "port" , port ) ; this . http = require ( "http" ) . Server ( this . exp ) ; this . io = socketIo ( this . http ) ; this . exp . get ( "/" , ( req , res ) => { res . sendFile ( __dirname + "/index.html" ) ; } ) ; this . http . listen ( port , ( ) => { console . log ( "listening on *:" + port ) ; } ) ; this . io . on ( "connection" , ( sock ) => { this . socket = sock ; callback ( ) ; console . log ( "user connected!" ) ; this . socket . on ( "disconnect" , ( ) => { console . log ( "user disconnected!" ) ; } ) ; } ) ; } catch ( error ) { console . log ( "couldn't start App Server! " + error ) ; } } } ; public sendPlayingStatus = ( paused ) => { if ( this . socket !== undefined ) { this . socket . emit ( "paused" , paused ) ; } } ; public setListener = ( socket , callback ) => { socket . on ( "playPause" , ( ) => { callback ( "playAndPause" , ( paused ) => { socket . emit ( "paused" , paused ) ; } ) ; } ) ; socket . on ( "nextTrack" , ( ) => { callback ( "nextTrack" , ( track ) => { socket . emit ( "setTrack" , track ) ; } ) ; } ) ; socket . on ( "previousTrack" , ( ) => { callback ( "previousTrack" , ( track ) => { socket . emit ( "setTrack" , track ) ; } ) ; } ) ; socket . on ( "connection" , ( ) => { callback ( "connected" , this . setTrackAndArtist ) ; } ) ; } ; public setTrack = ( track ) => { if ( this . socket !== undefined ) { this . socket . emit ( "setTrack" , track ) ; } } ; public setTrackAndArtist = ( track , artist ) => { if ( this . socket !== undefined ) { this . socket . emit ( "setTrackAndArtist" , [ track , artist ] ) ; } } ; public sendTracksInPlaylist = ( trackList ) => { if ( this . socket !== undefined ) { this . socket . emit ( "TracksInPlaylist" , trackList ) ; } } ; public sendTrackImage = ( image ) => { if ( this . socket !== undefined ) { this . socket . emit ( "TrackImage" , image ) ; } } ; }