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

Commit 2a3fb0a

Browse files
committed
cleanup, boilerplate frame parsing.
1 parent 06a00b5 commit 2a3fb0a

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

frame.js

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
var sys = require('sys');
22

33
function Frame() {
4-
this.connected = false;
5-
this.sock = '';
6-
this.command = '';
7-
this.headers = '';
8-
this.body = '';
9-
10-
this.stomp_connect = function (sock) {
11-
this.sock = sock;
12-
this.connected = true;
4+
var connected = false,
5+
sock = '',
6+
command = '',
7+
headers = '',
8+
body = '';
9+
10+
this.stomp_connect = function (client) {
11+
connected = true;
12+
sock = client;
1313
var args = Array();
1414
var headers = Array();
1515

@@ -22,21 +22,18 @@ function Frame() {
2222
};
2323

2424
this.build_frame = function(args, want_receipt) {
25-
this.command = args['command'];
26-
this.headers = args['headers'];
27-
this.body = args['body'];
25+
command = args['command'];
26+
headers = args['headers'];
27+
body = args['body'];
2828
if (want_receipt) {
2929
receipt_stamp = Math.floor(Math.random()*10000000+1);
30-
this.headers['receipt'] = "-"
30+
this.headers['receipt'] = "-"
3131
console.log(want_receipt);
3232
}
3333
return this;
3434
};
3535

3636
this.as_string = function() {
37-
command = this.command;
38-
headers = this.headers;
39-
body = this.body;
4037
header_strs = Array();
4138

4239
for (var header in headers) {
@@ -49,8 +46,29 @@ function Frame() {
4946
};
5047

5148
this.send_frame = function(frame) {
52-
this.sock.write(frame.as_string());
49+
console.dir(sock);
50+
sock.write(frame.as_string());
51+
};
52+
53+
this.parse_frame = function(data) {
54+
args = Array();
55+
headers = Array();
56+
headers_str = '';
57+
body = '';
58+
59+
command = this.parse_command(data);
60+
data = data.slice(command.len+1)
61+
headers_str, body = data.split("\n\n");
62+
headers = this.parse_headers(headers_str);
63+
64+
args['command'] = command;
65+
args['headers'] = headers;
66+
args['body'] = body;
67+
68+
this_frame = Frame(sock);
69+
this_frame.build_frame(args);
5370
};
71+
5472
};
5573

5674
module.exports = Frame;

stomp.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ var net = require('net');
22
var Frame = require('./frame');
33

44
function Stomp(port, host) {
5-
this.port = port;
6-
this.host = host;
7-
this.frame = new Frame();
5+
var port = port,
6+
host = host,
7+
frame = new Frame();
88

99
this.connect = function() {
10-
port = this.port;
11-
host = this.host;
12-
frame = this.frame;
1310

1411
console.log('Connecting to ' + host + ':' + port);
1512
client = net.createConnection(port, host);
@@ -22,7 +19,7 @@ function Stomp(port, host) {
2219
});
2320
client.addListener('data', function (data) {
2421
console.log("Got: " + data);
25-
console.dir(connected_frame);
22+
connected_frame.parse_frame(data);
2623
});
2724
client.addListener('end', function () {
2825
console.log('goodbye');

0 commit comments

Comments
 (0)