@ -19,15 +19,15 @@ class Client {
}
async connect ( ) {
if ( this . status ) {
return Promise . resolve ( { error : false , data : "" } ) ;
}
const connectionResponse = await connectSocket ( this . conn , this . port , this . host ) ;
this . status = connectionResponse . error === true ? 0 : 1 ;
return Promise . resolve ( connectionResponse ) ;
if ( this . status ) {
return Promise . resolve ( { error : false , data : "" } ) ;
}
const connectionResponse = await connectSocket ( this . conn , this . port , this . host ) ;
this . status = connectionResponse . error === true ? 0 : 1 ;
return Promise . resolve ( connectionResponse ) ;
}
close ( ) {
close ( ) {
if ( ! this . status ) {
return ;
}
@ -38,7 +38,7 @@ class Client {
request ( method , params ) {
if ( ! this . status ) {
return Promise . reject ( new Error ( 'ESOCKET ' ) ) ;
return Promise . reject ( new Error ( 'Connection to server lost, please retry ' ) ) ;
}
return new Promise ( ( resolve , reject ) => {
const id = ++ this . id ;
@ -47,10 +47,10 @@ class Client {
this . conn . write ( content + '\n' ) ;
} ) ;
}
requestBatch ( method , params , secondParam ) {
if ( ! this . status ) {
return Promise . reject ( new Error ( 'ESOCKET ' ) ) ;
return Promise . reject ( new Error ( 'Connection to server lost, please retry ' ) ) ;
}
return new Promise ( ( resolve , reject ) => {
let arguments_far_calls = { } ;
@ -70,7 +70,7 @@ class Client {
this . conn . write ( content + '\n' ) ;
} ) ;
}
response ( msg ) {
let callback ;
if ( ! msg . id && msg [ 0 ] && msg [ 0 ] . id ) {
@ -84,7 +84,7 @@ class Client {
} else {
callback = this . callback_message_queue [ msg . id ] ;
}
if ( callback ) {
delete this . callback_message_queue [ msg . id ] ;
if ( msg . error ) {
@ -97,15 +97,29 @@ class Client {
}
}
onMessage ( body , n ) {
onMessage ( body , n ) {
const msg = JSON . parse ( body ) ;
if ( msg instanceof Array ) {
this . response ( msg ) ;
} else {
if ( msg . id !== void 0 ) {
this . response ( msg )
} else {
this . subscribe . emit ( msg . method , msg . params ) ;
try {
const msg = JSON . parse ( body ) ;
if ( msg instanceof Array ) {
this . response ( msg ) ;
} else {
this . subscribe . emit ( msg . method , msg . params ) ;
if ( msg . id !== void 0 ) {
this . response ( msg ) ;
} else {
this . subscribe . emit ( msg . method , msg . params ) ;
}
}
} catch ( error ) {
this . conn . end ( ) ;
this . conn . destroy ( ) ;
this . onClose ( error ) ;
}
}
}
}
@ -124,12 +138,9 @@ class Client {
this . mp . run ( chunk )
}
onEnd ( ) {
}
onError ( e ) {
//console.log('OnError:' + e);
}
}
module . exports = Client ;