Permalink
Browse files

Adding DISCONNECT method and tests, and updated working example to us…

…e it
  • Loading branch information...
1 parent feb461b commit 7e5f743b22178affb06a2dcc2a79b473ddad5db9 @grenzr grenzr committed Mar 19, 2012
Showing with 73 additions and 4 deletions.
  1. +5 −0 examples/clientServerConnect.js
  2. +21 −1 lib/client.js
  3. +1 −1 package.json
  4. +46 −2 test/client.test.js
@@ -24,4 +24,9 @@ stompClient.connect(function() {
setTimeout(function(){
stompClient.publish(QUEUE, 'so wonely, so wonely and bwue!');
}, 4000);
+ setTimeout(function(){
+ stompClient.disconnect(function() {
+ console.log('DISCONNECTED');
+ });
+ }, 5000);
});
View
@@ -48,6 +48,23 @@ StompClient.prototype.connect = function(connectedCallback) {
});
};
+StompClient.prototype.disconnect = function(disconnectedCallback) {
+ var self = this;
+ if (this.stream) {
+
+ this.on('disconnect', disconnectedCallback);
+
+ var frame = new StompFrame({
+ command: 'DISCONNECT'
+ }).send(this.stream);
+
+ process.nextTick(function() {
+ self.stream.emit('end');
+ });
+
+ }
+};
+
StompClient.prototype.onConnect = function() {
var self = this;
@@ -61,7 +78,10 @@ StompClient.prototype.onConnect = function() {
self.stream.on('end', function() {
self.stream.end();
+ process.nextTick(function() {
self.emit('disconnect');
+ });
+
});
frameEmitter.on('MESSAGE', function(frame) {
@@ -98,7 +118,7 @@ StompClient.prototype.onConnect = function() {
};
StompClient.prototype.subscribe = function(queue, callback, headers) {
- headers && (headers["destination"] = queue) || (headers = {"destination": queue});
+ headers && (headers["destination"] = queue) || (headers = {"destination": queue});
if (!(queue in this.subscriptions)) {
this.subscriptions[queue] = [];
new StompFrame({
View
@@ -6,7 +6,7 @@
],
"name": "stomp-client",
"description": "A STOMP protocol implementation in node.js",
- "version": "0.2.0",
+ "version": "0.2.1",
"repository": {
"url": "http://github.com/easternbloc/node-stomp-client"
},
View
@@ -60,7 +60,7 @@ module.exports = testCase({
},
'check outbound CONNECT frame correctly follows protocol specification': function(test) {
-
+ var self = this;
test.expect(4);
sendHook = function(stompFrame) {
@@ -78,13 +78,14 @@ module.exports = testCase({
//start the test
this.stompClient.connect();
connectionObserver.emit('connect');
+
},
'check inbound CONNECTED frame parses correctly': function(test) {
var self = this;
var testId = '1234';
- test.expect(2);
+ test.expect(2);
sendHook = function() {
self.stompClient.stream.emit('data', 'CONNECTED\nsession:' + testId + '\n\n\0');
@@ -250,6 +251,49 @@ module.exports = testCase({
this.stompClient.connect(function() {});
connectionObserver.emit('connect');
+ },
+
+ 'check disconnect method correctly sends DISCONNECT frame, disconnects TCP stream, and fires callback': function (test) {
+ var self = this;
+
+ test.expect(7);
+
+ self.stompClient.on('disconnect', function() {
+ test.ok(true, 'disconnect event fired');
+ });
+
+ //mock that we received a CONNECTED from the stomp server in our send hook
+ sendHook = function (stompFrame) {
+ self.stompClient.stream.emit('data', 'CONNECTED\nsession:blah\n\n\0');
+ };
+
+ self.stompClient.connect(function() {
+
+ // Assert next outbound STOMP frame is a DISCONNECT
+ sendHook = function (stompFrame) {
+ test.equal(stompFrame.command, 'DISCONNECT');
+ test.deepEqual(stompFrame.headers, {});
+ test.equal(stompFrame.body, '');
+ };
+
+ self.stompClient.stream.on('end', function() {
+ test.ok(true, 'tcp stream end event is fired');
+ });
+
+ // Set disconnection callback to ensure it is called appropriately
+ self.stompClient.disconnect(function () {
+ test.ok(true, 'disconnect callback executed');
+ test.done();
+ });
+
+ });
+
+ // Mock the TCP end call
+ connectionObserver.end = function() {
+ test.ok(true, 'TCP end call made');
+ };
+
+ connectionObserver.emit('connect');
}
});

0 comments on commit 7e5f743

Please sign in to comment.