import * as io from 'socket.io-client' ; import { Packet } from '../../common/types' ; export class Client { public readonly url : string ; private socket : SocketIOClient . Socket ; constructor ( url , onPacket ) { this . url = url ; this . socket = io ( url , { reconnection : true } ) ; this . socket . on ( 'packet' , onPacket ) ; this . socket . on ( 'connect' , this . onConnect . bind ( this ) ) ; this . socket . on ( 'disconnect' , this . onDisconnect . bind ( this ) ) ; } public sendPacket ( packet ) { this . socket . emit ( 'packet' , packet ) ; } private onConnect ( ) { console . info ( 'Connected to server' ) ; } private onDisconnect ( ) { console . warn ( 'Disconnected from server' ) ; } }