Skip to content

Commit

Permalink
Merge 7a5b2f7 into 6c24e51
Browse files Browse the repository at this point in the history
  • Loading branch information
youngtt committed Sep 1, 2017
2 parents 6c24e51 + 7a5b2f7 commit 204c42a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/http-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,10 @@ HttpContext.prototype.respondWithEventStream = function(stream) {
stream.on('end', function() {
client.send({event: 'end', data: 'null'});
});

client.on('close', function() {
stream.destroy();
});
};

/**
Expand Down
37 changes: 36 additions & 1 deletion test/streams.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('strong-remoting', function() {
describe('a function returning a ReadableStream', function() {
var Readable = require('stream').Readable;
var remotes = RemoteObjects.create();
var streamClass, server, app;
var streamClass, server, app, streamClosed;

before(function(done) {
var test = this;
Expand Down Expand Up @@ -99,6 +99,22 @@ describe('a function returning a ReadableStream', function() {
cb(null, rs);
};

StreamClass.createInfiniteStream = function createStream(cb) {
streamClosed = new Promise(resolve => {
const rs = new Readable({
objectMode: true,
read: function(size) {
setTimeout(() => this.push({foo: 'bar'}), 50);
},
destroy: function(size) {
resolve(true);
},
});

cb(null, rs);
});
};

streamClass = new SharedClass('StreamClass', StreamClass);

this.createStreamMethod = streamClass.defineMethod('createStream', {
Expand All @@ -121,6 +137,16 @@ describe('a function returning a ReadableStream', function() {
}],
});

streamClass.defineMethod('createInfiniteStream', {
isStatic: true,
fn: StreamClass.createInfiniteStream,
returns: [{
arg: 'result',
type: 'ReadableStream',
json: true,
}],
});

remotes.addClass(streamClass);
app.use(remotes.handler('rest'));
});
Expand Down Expand Up @@ -194,6 +220,15 @@ describe('a function returning a ReadableStream', function() {
});
});

it('should close server stream on client disconnect', function(done) {
const es = new EventSource(this.url + '/StreamClass/createInfiniteStream');

es.on('data', function(e) {
es.close();
streamClosed.then(() => done());
});
});

after(function() {
this.server.close();
});
Expand Down

0 comments on commit 204c42a

Please sign in to comment.