Skip to content
This repository was archived by the owner on Nov 28, 2018. It is now read-only.

Commit 126173a

Browse files
committed
A bit of cleanup for readability and standardization. Add a few basic methods to stomp. Don't throw exceptions, instead emit an error event (cleaner and more node like..).
1 parent d828987 commit 126173a

File tree

6 files changed

+96
-50
lines changed

6 files changed

+96
-50
lines changed

lib/frame.js

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ var sys = require('sys'),
33
exceptions = require('./stomp-exceptions');
44

55
Frame = module.exports = function(logger) {
6-
this.me = Math.floor(Math.random()*10000000+1);
76
this.sock = null;
87
this.command = null;
98
this.headers = null;
@@ -44,20 +43,20 @@ Frame.prototype.build_frame = function(args, want_receipt) {
4443
this.body = args['body'];
4544

4645
if (want_receipt) {
47-
receipt_stamp = Math.floor(Math.random()*10000000+1);
46+
receipt_stamp = Math.floor(Math.random()*99999999999).toString();
4847
if (this.session != null) {
4948
this.headers['receipt'] = receipt_stamp + "-" + this.session;
5049
}
5150
else {
52-
this.headers['receipt'] = receipt_stamp + "-";
51+
this.headers['receipt'] = receipt_stamp;
5352
}
5453
}
5554
return this;
5655
};
5756

5857
Frame.prototype.as_string = function() {
5958
var self = this,
60-
header_strs = Array(),
59+
header_strs = [],
6160
frame = null,
6261
command = this.command,
6362
headers = this.headers,
@@ -83,7 +82,7 @@ Frame.prototype.send_frame = function(frame) {
8382

8483
Frame.prototype.parse_frame = function(data) {
8584
var self = this,
86-
args = [],
85+
args = {},
8786
headers_str = null;
8887

8988
if (!this.utils.really_defined(data))
@@ -100,9 +99,6 @@ Frame.prototype.parse_frame = function(data) {
10099
if ('content-length' in this.headers)
101100
this.headers['bytes_message'] = true;
102101

103-
if (this.command == 'ERROR')
104-
throw new BrokerErrorResponse(this.body);
105-
106102
args['command'] = this.command;
107103
args['headers'] = this.headers;
108104
args['body'] = this.body;
@@ -113,8 +109,8 @@ Frame.prototype.parse_frame = function(data) {
113109
};
114110

115111
Frame.prototype.parse_headers = function(headers_str) {
116-
var these_headers = new Array(),
117-
one_header = new Array(),
112+
var these_headers = [],
113+
one_header = [],
118114
header_key = null,
119115
header_val = null,
120116
headers_split = headers_str.split('\n');

lib/stomp-exceptions.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,3 @@
1-
BrokerErrorResponse = exports.BrokerErrorResponse = function(value) {
2-
this.value = value;
3-
this.message = "Broker returned error: " + this.value;
4-
};
5-
6-
BrokerErrorResponse.prototype.toString = function() {
7-
return this.messge + this.value;
8-
};
9-
101
QueueEmpty = exports.QueueEmpty = function() {
112
this.name = "QueueEmpty";
123
this.message = "Queue is Empty";

lib/stomp.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var net = require('net'),
44
utils = require('./stomp-utils'),
55
exceptions = require('./stomp-exceptions');
66

7-
function Stomp(port, host, debug, conf) {
7+
function Stomp(port, host, debug) {
88
this.socket = null;
99
this.port = port || 61613;
1010
this.host = host || "127.0.0.1";
@@ -14,7 +14,7 @@ function Stomp(port, host, debug, conf) {
1414
this.frame_object = new Frame(this.stomp_log);
1515
this.utils = new StompUtils();
1616
this.connected_frame = null;
17-
this._subscribed_to = {};
17+
this._subscribed_to = [];
1818
}
1919

2020
Stomp.prototype = new process.EventEmitter();
@@ -64,6 +64,13 @@ Stomp.prototype.connect = function(headers) {
6464
this.socket = socket;
6565
};
6666

67+
Stomp.prototype.disconnect = function() {
68+
69+
this.socket.end();
70+
if (this.socket.readyState == 'readOnly')
71+
this.socket.destroy();
72+
73+
}
6774

6875
Stomp.prototype.subscribe = function(headers, callback) {
6976

@@ -83,6 +90,13 @@ Stomp.prototype.unsubscribe = function(headers) {
8390

8491
};
8592

93+
Stomp.prototype.ack = function(message_id) {
94+
95+
this._send_command('ACK', {'message-id': message_id});
96+
this.stomp_log.debug('acknowledged message: ' + message_id);
97+
98+
};
99+
86100
Stomp.prototype.send = function(headers) {
87101

88102
destination = headers['destination'];
@@ -94,9 +108,13 @@ Stomp.prototype.send = function(headers) {
94108
Stomp.prototype._send_command = function(command, headers, extra) {
95109

96110
if (!this.utils.really_defined(headers))
97-
headers = {};
111+
headers = [];
112+
113+
frame_conf = {
114+
'command': command,
115+
'headers': headers
116+
};
98117

99-
frame_conf = {'command': command, 'headers': headers};
100118
this_frame = this.connected_frame.build_frame(frame_conf);
101119
reply = this.connected_frame.send_frame(this_frame);
102120

stomp-client.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

stomp-consumer.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#!/usr/bin/env node
2+
3+
var stomp = require('./lib/stomp');
4+
5+
//{'login': 'bah', 'password': 'bah'}
6+
var client = new stomp.Stomp(61613, 'localhost', true);
7+
8+
var headers = {
9+
'destination': '/queue/test',
10+
'ack': 'auto'
11+
};
12+
13+
var messages = [];
14+
15+
client.connect();
16+
17+
client.on('connected', function() {
18+
client.subscribe(headers);
19+
console.log('Connected');
20+
});
21+
22+
client.on('message', function(message) {
23+
messages.push(message);
24+
});
25+
26+
client.on('error', function(error_frame) {
27+
console.log(error_frame.body);
28+
});
29+
30+
process.on('SIGINT', function() {
31+
console.log(messages.length);
32+
client.disconnect();
33+
});

stomp-producer.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/env node
2+
3+
var stomp = require('./lib/stomp');
4+
5+
var num = process.argv[2];
6+
7+
//{'login': 'bah', 'password': 'bah'}
8+
var client = new stomp.Stomp(61613, 'localhost', true);
9+
10+
var queue = '/queue/test';
11+
12+
client.connect();
13+
14+
client.on('connected', function() {
15+
if (!num) num = 10;
16+
17+
for (var i = 0; i < num; i++) {
18+
console.log(i);
19+
client.send({
20+
'destination': queue,
21+
'body': 'Testing' + i,
22+
'persistent': 'true'
23+
});
24+
}
25+
client.disconnect();
26+
});
27+
28+
client.on('error', function(error_frame) {
29+
console.log(error_frame.body);
30+
client.disconnect();
31+
});
32+
33+
process.on('SIGINT', function() {
34+
client.disconnect();
35+
});

0 commit comments

Comments
 (0)