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

Commit a300e9f

Browse files
author
Tony Young
committed
Move socket out of global scope into STOMP object.
1 parent ecb10cb commit a300e9f

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

lib/stomp.js

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ var net = require('net'),
2626
stomp_utils = require('./stomp-utils'),
2727
exceptions = require('./stomp-exceptions'),
2828
utils = new stomp_utils.StompUtils(),
29-
socket = net.Stream(),
3029
log = null;
3130

3231
function parse_command(data) {
@@ -92,6 +91,7 @@ function parse_frame(chunk) {
9291
};
9392

9493
function _connect(stomp) {
94+
var socket = stomp.socket = net.Stream();
9595
log = stomp.log;
9696
log.debug('Connecting to ' + stomp.host + ':' + stomp.port);
9797
socket.connect(stomp.port, stomp.host);
@@ -100,10 +100,10 @@ function _connect(stomp) {
100100
log.debug('Connected to socket');
101101
if (utils.really_defined(stomp.login)
102102
&& utils.really_defined(stomp.passcode)) {
103-
stomp_connect({login: stomp.login, passcode: stomp.passcode});
103+
stomp_connect(stomp, {login: stomp.login, passcode: stomp.passcode});
104104
}
105105
else {
106-
stomp_connect();
106+
stomp_connect(stomp);
107107
}
108108
});
109109

@@ -141,7 +141,7 @@ function _connect(stomp) {
141141
});
142142
};
143143

144-
function stomp_connect(headers) {
144+
function stomp_connect(stomp, headers) {
145145
var _frame = new frame.Frame(),
146146
args = {},
147147
headers = headers || {};
@@ -150,18 +150,19 @@ function stomp_connect(headers) {
150150
args['headers'] = headers;
151151

152152
var frame_to_send = _frame.build_frame(args);
153-
send_frame(frame_to_send);
153+
send_frame(stomp, frame_to_send);
154154
log.debug('Connected to STOMP');
155155
};
156156

157-
function _disconnect() {
157+
function _disconnect(stomp) {
158+
var socket = stomp.socket;
158159
socket.end();
159160
if (socket.readyState == 'readOnly')
160161
socket.destroy();
161162
log.debug('disconnect called');
162163
};
163164

164-
function send_command(command, headers, body, want_receipt) {
165+
function send_command(stomp, command, headers, body, want_receipt) {
165166
var want_receipt = want_receipt || false;
166167
if (!utils.really_defined(headers))
167168
headers = {};
@@ -174,12 +175,13 @@ function send_command(command, headers, body, want_receipt) {
174175

175176
var _frame = new frame.Frame();
176177
var this_frame = _frame.build_frame(args, want_receipt);
177-
send_frame(this_frame);
178+
send_frame(stomp, this_frame);
178179

179180
return this_frame;
180181
};
181182

182-
function send_frame(_frame) {
183+
function send_frame(stomp, _frame) {
184+
var socket = stomp.socket;
183185
var frame_str = _frame.as_string();
184186

185187
if (socket.write(frame_str) === false) {
@@ -246,7 +248,7 @@ Stomp.prototype.handle_new_frame = function(this_frame) {
246248
* Disconnect from STOMP broker
247249
*/
248250
Stomp.prototype.disconnect = function() {
249-
_disconnect();
251+
_disconnect(this);
250252
}
251253

252254
/**
@@ -257,7 +259,7 @@ Stomp.prototype.subscribe = function(headers) {
257259
var self = this;
258260
destination = headers['destination'];
259261
headers['session'] = self.session;
260-
send_command('SUBSCRIBE', headers);
262+
send_command(this, 'SUBSCRIBE', headers);
261263
self._subscribed_to[destination] = true;
262264
self.log.debug('subscribed to: ' + destination + ' with headers ' + sys.inspect(headers));
263265
};
@@ -270,7 +272,7 @@ Stomp.prototype.unsubscribe = function(headers) {
270272
var self = this;
271273
destination = headers['destination'];
272274
headers['session'] = self.session;
273-
send_command('UNSUBSCRIBE', headers);
275+
send_command(this, 'UNSUBSCRIBE', headers);
274276
self._subscribed_to[destination] = false;
275277
self.log.debug('no longer subscribed to: ' + destination);
276278
};
@@ -281,7 +283,7 @@ Stomp.prototype.unsubscribe = function(headers) {
281283
*/
282284
Stomp.prototype.ack = function(message_id) {
283285
var self = this;
284-
send_command('ACK', {'message-id': message_id});
286+
send_command(this, 'ACK', {'message-id': message_id});
285287
self.log.debug('acknowledged message: ' + message_id);
286288
};
287289

@@ -292,7 +294,7 @@ Stomp.prototype.ack = function(message_id) {
292294
Stomp.prototype.begin = function() {
293295
var self = this;
294296
transaction_id = Math.floor(Math.random()*99999999999).toString();
295-
send_command('BEGIN', {'transaction': transaction_id});
297+
send_command(this, 'BEGIN', {'transaction': transaction_id});
296298
self.log.debug('begin transaction: ' + transaction_id);
297299
return transaction_id;
298300
};
@@ -303,7 +305,7 @@ Stomp.prototype.begin = function() {
303305
*/
304306
Stomp.prototype.commit = function(transaction_id) {
305307
var self = this;
306-
send_command('COMMIT', {'transaction': transaction_id});
308+
send_command(this, 'COMMIT', {'transaction': transaction_id});
307309
self.log.debug('commit transaction: ' + transaction_id);
308310
};
309311

@@ -313,7 +315,7 @@ Stomp.prototype.commit = function(transaction_id) {
313315
*/
314316
Stomp.prototype.abort = function(transaction_id) {
315317
var self = this;
316-
send_command('ABORT', {'transaction': transaction_id});
318+
send_command(this, 'ABORT', {'transaction': transaction_id});
317319
self.log.debug('abort transaction: ' + transaction_id);
318320
};
319321

@@ -328,7 +330,7 @@ Stomp.prototype.send = function(headers, want_receipt) {
328330
destination = headers['destination'];
329331
body = headers['body'] || null;
330332
headers['session'] = self.session;
331-
return send_command('SEND', headers, body, want_receipt)
333+
return send_command(this, 'SEND', headers, body, want_receipt)
332334
};
333335

334336
module.exports.Stomp = Stomp;

0 commit comments

Comments
 (0)