Last active
May 3, 2018 15:32
-
-
Save hendrikhofstadt/2e8b155034a536b62b303ae6d4f17d8b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as socketCluster from 'socketcluster-client' | |
import * as request from 'request-promise' | |
import {RequestPromise} from 'request-promise' | |
import WAMPClient = require('wamp-socket-cluster/WAMPClient'); | |
export class LiskClient { | |
socket: any; | |
public options = { | |
hostname: 'betanet.lisk.io', | |
port: 5001, | |
httpPort: 5000, | |
query: { | |
nonce: 'aaaaaaaaaaaaaaaa', | |
nethash: 'ef3844327d1fd0fc5785291806150c937797bdb34a748c9cd932b7e859e9ca0c', | |
version: '1.0.0', | |
os: 'linux', | |
height: 500, | |
wsPort: 500, | |
httpPort: 500, | |
}, | |
connectTimeout: 2000, | |
ackTimeout: 2000, | |
pingTimeout: 2000, | |
connectAttempts: 1, | |
}; | |
constructor(hostname, wsPort, httpPort) { | |
this.options.hostname = hostname || 'betanet.lisk.io'; | |
this.options.port = wsPort || 5001; | |
this.options.httpPort = httpPort || 5000; | |
} | |
public connect(errorHandler) { | |
return new Promise((resolve, reject) => { | |
// Initiate the connection to the server | |
let rpcClient = new WAMPClient(5000); | |
this.socket = socketCluster.create(this.options); | |
rpcClient.upgradeToWAMP(this.socket); | |
this.socket.on('connect', resolve); | |
this.socket.on('error', (err) => { | |
this.socket.destroy(); | |
errorHandler(err); | |
}); | |
}) | |
} | |
public getStatus(): Promise<NodeStatus> { | |
return this.socket.call('status'); | |
} | |
public getStatusDetailed(): RequestPromise { | |
return request(`http://${this.options.hostname}:${this.options.httpPort}/api/node/status`); | |
} | |
public getPeers(): Promise<any> { | |
return this.socket.call('list'); | |
} | |
} | |
export type NodeStatus = { | |
success: boolean; | |
height: number; | |
broadhash: string; | |
nonce: string; | |
httpPort: number; | |
version: string; | |
os: string; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment