-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathframe.js
49 lines (43 loc) · 1.18 KB
/
frame.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function StompFrame(frame) {
if (frame == undefined) {
frame = {};
}
this.command = frame.command || '';
this.headers = frame.headers || {};
this.body = frame.body || '';
this.contentLength = -1;
};
StompFrame.prototype.toString = function() {
return JSON.stringify({
command: this.command,
headers: this.headers,
body: this.body,
});
};
StompFrame.prototype.send = function(stream) {
stream.write(this.command + '\n');
for (var key in this.headers) {
stream.write(key + ':' + this.headers[key] + '\n');
}
if (this.body.length > 0) {
stream.write('content-length:' + this.body.length + '\n');
}
stream.write('\n');
if (this.body.length > 0) {
stream.write(this.body);
}
stream.write('\0');
};
StompFrame.prototype.setCommand = function(command) {
this.command = command;
};
StompFrame.prototype.setHeader = function(key, value) {
this.headers[key] = value;
if (key.toLowerCase() == 'content-length') {
this.contentLength = parseInt(value);
}
};
StompFrame.prototype.appendToBody = function(data) {
this.body += data;
};
exports.StompFrame = StompFrame;