pow/pow_service.js
function connectToServer( oOptions )
{
	let sUrl;
	let oWs;

	if ( 'object' !== typeof oOptions )
	{
		throw new Error( 'call connectToServer with invalid oOptions' );
	}
	if ( 'string' !== typeof oOptions.minerGateway ||
		0 === oOptions.minerGateway.length )
	{
		throw new Error( 'call connectToServer with invalid oOptions.minerGateway' );
	}
	if ( 'function' !== typeof oOptions.onOpen )
	{
		throw new Error( 'call connectToServer with invalid oOptions.onOpen' );
	}
	if ( 'function' !== typeof oOptions.onMessage )
	{
		throw new Error( 'call connectToServer with invalid oOptions.onMessage' );
	}
	if ( 'function' !== typeof oOptions.onError )
	{
		throw new Error( 'call connectToServer with invalid oOptions.onError' );
	}
	if ( 'function' !== typeof oOptions.onClose )
	{
		throw new Error( 'call connectToServer with invalid oOptions.onClose' );
	}

	//	...
	sUrl	= oOptions.minerGateway;
	sUrl	= sUrl.trim().toLowerCase();
	oWs	= new WebSocket( sUrl );

	//
	//	set max listeners for EventEmitter
	//
	oWs.on( 'open', () =>
	{
		let oAnotherWsToTheSameServer;

		if ( ! oWs.url )
		{
			throw new Error( "no url on ws" );
		}

		//
		//	browser implementation of Web Socket might add '/'
		//
		if ( oWs.url !== sUrl && oWs.url !== sUrl + "/" )
		{
			throw new Error( `url is different: ${ oWs.url }` );
		}

		//	...
		oAnotherWsToTheSameServer	= _cacheGetHandleByUrl( sUrl );
		if ( oAnotherWsToTheSameServer )
		{
			//
			//	duplicate driver.
			//	May happen if we abondoned a driver attempt after timeout
			// 		but it still succeeded while we opened another driver
			//
			console.log( `already have a connection to ${ sUrl }, will keep the old one and close the duplicate` );
			oWs.close( 1000, 'duplicate driver' );

			//	...
			return oOptions.onOpen( null, oWs );
		}

		//
		//	almost done!
		//
		oWs.peer	= sUrl;				//	peer
		oWs.host	= _getHostByPeerUrl( sUrl );	//	host
		oWs.bOutbound	= true;				//	identify this driver as outbound driver
		oWs.last_ts	= Date.now();			//	record the last timestamp while we connected to this peer

		//	...
		console.log( `connected to ${ sUrl }, host ${ oWs.host }` );

		//
		//	cache new socket handle
		//
		_cacheAddHandle( oWs );

		//	...
		return oOptions.onOpen( null, oWs );
	});
	oWs.on( 'message', ( sMessage ) =>
	{
		oOptions.onMessage( oWs, sMessage );
	});
	oWs.on( 'close', () =>
	{
		_cacheRemoveHandle( oWs );
		oOptions.onClose( oWs, `socket was close` );
	});
	oWs.on( 'error', ( vError ) =>
	{
		oOptions.onError( oWs, vError );
	});

}
function connectToServer( oOptions )
{
	let sUrl;
	let oWs;

	if ( 'object' !== typeof oOptions )
	{
		throw new Error( 'call connectToServer with invalid oOptions' );
	}
	if ( 'string' !== typeof oOptions.minerGateway ||
		0 === oOptions.minerGateway.length )
	{
		throw new Error( 'call connectToServer with invalid oOptions.minerGateway' );
	}
	if ( 'function' !== typeof oOptions.onOpen )
	{
		throw new Error( 'call connectToServer with invalid oOptions.onOpen' );
	}
	if ( 'function' !== typeof oOptions.onMessage )
	{
		throw new Error( 'call connectToServer with invalid oOptions.onMessage' );
	}
	if ( 'function' !== typeof oOptions.onError )
	{
		throw new Error( 'call connectToServer with invalid oOptions.onError' );
	}
	if ( 'function' !== typeof oOptions.onClose )
	{
		throw new Error( 'call connectToServer with invalid oOptions.onClose' );
	}

	//	...
	sUrl	= oOptions.minerGateway;
	sUrl	= sUrl.trim().toLowerCase();
	oWs	= new WebSocket( sUrl );

	//
	//	set max listeners for EventEmitter
	//
	oWs.on( 'open', () =>
	{
		let oAnotherWsToTheSameServer;

		if ( ! oWs.url )
		{
			throw new Error( "no url on ws" );
		}

		//
		//	browser implementation of Web Socket might add '/'
		//
		if ( oWs.url !== sUrl && oWs.url !== sUrl + "/" )
		{
			throw new Error( `url is different: ${ oWs.url }` );
		}

		//	...
		oAnotherWsToTheSameServer	= _cacheGetHandleByUrl( sUrl );
		if ( oAnotherWsToTheSameServer )
		{
			//
			//	duplicate driver.
			//	May happen if we abondoned a driver attempt after timeout
			// 		but it still succeeded while we opened another driver
			//
			console.log( `already have a connection to ${ sUrl }, will keep the old one and close the duplicate` );
			oWs.close( 1000, 'duplicate driver' );

			//	...
			return oOptions.onOpen( null, oWs );
		}

		//
		//	almost done!
		//
		oWs.peer	= sUrl;				//	peer
		oWs.host	= _getHostByPeerUrl( sUrl );	//	host
		oWs.bOutbound	= true;				//	identify this driver as outbound driver
		oWs.last_ts	= Date.now();			//	record the last timestamp while we connected to this peer

		//	...
		console.log( `connected to ${ sUrl }, host ${ oWs.host }` );

		//
		//	cache new socket handle
		//
		_cacheAddHandle( oWs );

		//	...
		return oOptions.onOpen( null, oWs );
	});
	oWs.on( 'message', ( sMessage ) =>
	{
		oOptions.onMessage( oWs, sMessage );
	});
	oWs.on( 'close', () =>
	{
		_cacheRemoveHandle( oWs );
		oOptions.onClose( oWs, `socket was close` );
	});
	oWs.on( 'error', ( vError ) =>
	{
		oOptions.onError( oWs, vError );
	});

}
function connectToServer ( oOptions ) { let sUrl ; let oWs ; if ( 'object' !== typeof oOptions ) { throw new Error ( 'call connectToServer with invalid oOptions' ) ; } if ( 'string' !== typeof oOptions . minerGateway || 0 === oOptions . minerGateway . length ) { throw new Error ( 'call connectToServer with invalid oOptions.minerGateway' ) ; } if ( 'function' !== typeof oOptions . onOpen ) { throw new Error ( 'call connectToServer with invalid oOptions.onOpen' ) ; } if ( 'function' !== typeof oOptions . onMessage ) { throw new Error ( 'call connectToServer with invalid oOptions.onMessage' ) ; } if ( 'function' !== typeof oOptions . onError ) { throw new Error ( 'call connectToServer with invalid oOptions.onError' ) ; } if ( 'function' !== typeof oOptions . onClose ) { throw new Error ( 'call connectToServer with invalid oOptions.onClose' ) ; } //	... sUrl = oOptions . minerGateway ; sUrl = sUrl . trim ( ) . toLowerCase ( ) ; oWs = new WebSocket ( sUrl ) ; // //	set max listeners for EventEmitter // oWs . on ( 'open' , ( ) => { let oAnotherWsToTheSameServer ; if ( ! oWs . url ) { throw new Error ( "no url on ws" ) ; } // //	browser implementation of Web Socket might add '/' // if ( oWs . url !== sUrl && oWs . url !== sUrl + "/" ) { throw new Error ( ` ${ oWs . url } ` ) ; } //	... oAnotherWsToTheSameServer = _cacheGetHandleByUrl ( sUrl ) ; if ( oAnotherWsToTheSameServer ) { // //	duplicate driver. //	May happen if we abondoned a driver attempt after timeout // 		but it still succeeded while we opened another driver // console . log ( ` ${ sUrl } ` ) ; oWs . close ( 1000 , 'duplicate driver' ) ; //	... return oOptions . onOpen ( null , oWs ) ; } // //	almost done! // oWs . peer = sUrl ; //	peer oWs . host = _getHostByPeerUrl ( sUrl ) ; //	host oWs . bOutbound = true ; //	identify this driver as outbound driver oWs . last_ts = Date . now ( ) ; //	record the last timestamp while we connected to this peer //	... console . log ( ` ${ sUrl } ${ oWs . host } ` ) ; // //	cache new socket handle // _cacheAddHandle ( oWs ) ; //	... return oOptions . onOpen ( null , oWs ) ; } ) ; oWs . on ( 'message' , ( sMessage ) => { oOptions . onMessage ( oWs , sMessage ) ; } ) ; oWs . on ( 'close' , ( ) => { _cacheRemoveHandle ( oWs ) ; oOptions . onClose ( oWs , ` ` ) ; } ) ; oWs . on ( 'error' , ( vError ) => { oOptions . onError ( oWs , vError ) ; } ) ; }
CLIENT
connect to server

@public
@param	{object}	oOptions
@param	{string}	oOptions.minerGateway			e.g. : wss://1.miner.trustnote.org
@param	{function}	oOptions.onOpen( err, oWsClient )
@param	{function}	oOptions.onMessage( oWsClient, sMessage )
@param	{function}	oOptions.onError( oWsClient, vError )
@param	{function}	oOptions.onClose( oWsClient, sReason )
CLIENT connect to server