UNPKG

mikesoftp3

Version:

A simple, easy-to-use library for connecting to the Mikesoft P3 network.

204 lines (184 loc) 4.74 kB
export declare class P3 { /** * Creates and sets up a P3 instance. * @param options The secret or P3 configuration options. */ constructor(options?:string|{ url?:string, secret?:string, autoinit?:boolean }) /** * Listens for an event. * @param event The name of the event. * @param handler The function to call when the event is triggered. */ on<K extends keyof P3EventMap>(event:K,handler:(event:P3EventMap[K])=>any):void /** * Closes the port and stops hosting on the specified port. * @param port The port to stop listening on. */ endPort(port:number):void /** * Starts hosting on the specified port. * @param port The port to listen on. * @param callback The function to call when a client connects. */ listen(port:number,callback:(peer:P3ClientEvent)=>any):void /** * Your current P3 address. */ adr:string /** * Wether the instance is connected to the P3 network. */ active:boolean /** * Your P3 secret used to keep your P3 address. */ key:string /** * Creates a client that can connect to a P3 server. * @param address The address of the host to connect to. * @param port The port on the server to access. */ createClient(address:string,port:string|number):P3Client /** * Starts the P3 instance. */ start():void /** * Stops all ongoing P3 connections. */ stop():void } declare class P3Client { /** * Gives information about the server you are connected to. */ server:P3ServerPeer /** * Listens for an event on the client. * @param event The name of the event to listen for. * @param handler The function to call when the event is triggered. */ on<K extends keyof P3ClientEventMap>(event:K,handler:(event:P3ClientEventMap[K])=>any):void /** * Ends the connection with the server. */ end():void /** * Sends data to the server the client is connected to. * @param data The data to send to the server. */ emit(data:any):void /** * Wether the connection has been ended. */ ended:boolean } interface P3ClientEventMap { 'connect':P3ServerConnectEvent, 'disconnect':P3ServerEndEvent, 'message':P3ServerResponseEvent, } declare class P3ServerResponseEvent { /** * The data sent to the client. */ data:any } declare class P3ServerConnectEvent { /** * The interval in which a heartbeat must be sent to prevent the server from ending the connection. */ heartbeatInterval:number /** * The peer ID sent by the server. This can also be accessed via `P3Client.server.peerID`. */ peerID:string } declare class P3ServerEndEvent { /** * Wether the connection was force-ended (eg relay server crash, internet loss) */ force:boolean } declare class P3ServerPeer { /** * The P3 address of the server. */ address:string /** * The port of the server you are connected to. */ port:number /** * The address and port of the server, seperated by a colon. */ destination:string /** * The peer ID generated by the client. */ id:string } declare class P3ClientInputEvent { /** * The data recieved from the client. */ data:any } declare class P3ClientEvent { /** * Contains information about the connected peer. */ peer:P3ClientPeer /** * * @param event The name of the event. * @param handler The function to call when the event is triggered. */ on<K extends keyof P3PeerEventMap>(event:K,handler:(event:P3PeerEventMap[K])=>any):void /** * Sends data to the connected client. * @param data The data to send to the client. */ emit(data:any):void /** * Returns the peer ID generated by the client. */ peerID:string } declare class P3ClientPeer { /** * The P3 address of the connected client. */ adr:string /** * The response port of the connected client. */ port:number /** * Sends data to the client. * @deprecated Use P3ClientEvent.emit instead of P3ClientEvent.peer.emit. * @param data The data to transmit. */ emit(data:any):void } declare class P3ClientEndEvent { } interface P3PeerEventMap { 'disconnect':P3ClientEndEvent; 'message':P3ClientInputEvent; } interface P3EventMap { 'connect':P3ConnectEvent; 'fail': P3FailEvent; } declare class P3ConnectEvent { address:string } declare class P3FailEvent { reason:string code:string }