Simplify the code of HttpRequest

This commit is contained in:
斟酌 鵬兄 2016-02-12 00:16:08 +08:00
parent 4e200c88a9
commit a83d3d9469

View File

@ -7,8 +7,9 @@ var https = require( "https" );
class HttpRequestCompleteEventArgs class HttpRequestCompleteEventArgs
{ {
constructor( ResponseData ) constructor( statusCode, ResponseData )
{ {
this.statusCode = statusCode;
this.Data = ResponseData; this.Data = ResponseData;
} }
@ -91,20 +92,18 @@ class HttpRequest extends EventEmitter
OnResponseReceived( Response ) OnResponseReceived( Response )
{ {
this.ResponseData = new Buffer( 0 ); var _self = this;
Response.addListener( "data", this.OnResponseData.bind( this ) ); var ResponseData = new Buffer( 0 );
Response.addListener( "end", this.OnResponseComplete.bind( this ) );
}
OnResponseComplete() Response.addListener( "data",
{ Data => ResponseData = Buffer.concat([ ResponseData, Data ])
this.emit( "RequestComplete" );
, this, new HttpRequestCompleteEventArgs( this.ResponseData ) );
}
OnResponseData( Data ) Response.addListener( "end", () => {
{ _self.emit( "RequestComplete"
this.ResponseData = Buffer.concat([ this.ResponseData, Data ]); , this, new HttpRequestCompleteEventArgs( Response.statusCode, ResponseData )
);
} );
} }
} }