Skip to content

Commit de848eb

Browse files
committed
reset the default session for a connection if that session is closed
1 parent 174beb9 commit de848eb

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

lib/connection.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ function session_per_connection(conn) {
8282
'get_session' : function () {
8383
if (!ssn) {
8484
ssn = conn.create_session();
85+
ssn.observers.on('session_close', function () { ssn = null; });
8586
ssn.begin();
8687
}
8788
return ssn;

lib/session.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ var Session = function (connection, local_channel) {
410410
this.links = {}; // map by name
411411
this.options = {};
412412
Object.defineProperty(this, 'error', { get: function() { return this.remote.end ? this.remote.end.error : undefined; }});
413+
this.observers = new EventEmitter();
413414
};
414415
Session.prototype = Object.create(EventEmitter.prototype);
415416
Session.prototype.constructor = Session;
@@ -436,6 +437,7 @@ Session.prototype._reconnect = function() {
436437

437438
Session.prototype.dispatch = function(name) {
438439
log.events('[%s] Session got event: %s', this.connection.options.id, name);
440+
EventEmitter.prototype.emit.apply(this.observers, arguments);
439441
if (this.listeners(name).length) {
440442
EventEmitter.prototype.emit.apply(this, arguments);
441443
return true;

test/links.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,4 +693,32 @@ describe('miscellaneous', function() {
693693
});
694694
client.connect(listener.address() as any).open_receiver();
695695
});
696+
697+
it('handles session close', function(done: Function) {
698+
var closed_session = false;
699+
var outgoing = ['one', 'two'] as any;
700+
var incoming = [] as any;
701+
server.on('sender_open', function(context: rhea.EventContext) {
702+
context.sender!.send({subject:outgoing.shift()} as rhea.Message);
703+
});
704+
server.once('accepted', function(context: rhea.EventContext) {
705+
closed_session = true;
706+
context.session!.close();
707+
});
708+
client.on('session_close', function (context: rhea.EventContext) {
709+
context.connection.open_receiver();
710+
});
711+
client.on('message', function (context: rhea.EventContext) {
712+
incoming.push(context.message!.subject);
713+
if (incoming.length == 2) {
714+
context.connection.close();
715+
}
716+
});
717+
client.on('connection_close', function (context: rhea.EventContext) {
718+
assert.deepEqual(incoming, ['one', 'two']);
719+
done();
720+
});
721+
var conn = client.connect({port: (listener.address() as any).port, id: 'client'});
722+
conn.open_receiver();
723+
});
696724
});

0 commit comments

Comments
 (0)