Skip to content
This repository has been archived by the owner on Sep 2, 2021. It is now read-only.

Send binary Brackets-Node command responses #406

Merged
merged 1 commit into from
Jan 9, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions appshell/node-core/ConnectionManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ maxerr: 50, node: true */
}
}
};

/**
* @private
* Sends a binary message over the WebSocket. Implicitly interpreted as a
* message of type "commandResponse".
* @param {Buffer} message
*/
Connection.prototype._sendBinary = function (message) {
if (this._ws && this._connected) {
this._ws.send(message, {binary: true, mask: false});
}
};

/**
* @private
Expand Down Expand Up @@ -138,15 +150,29 @@ maxerr: 50, node: true */
* Sends a response to a command execution
* @param {number} id unique ID of the command that was executed. ID is
* generated by the client when the command is issued.
* @param {object} response Result of the command execution. Must be
* JSON.stringify-able.
* @param {object|Buffer} response Result of the command execution. Must
* either be JSON.stringify-able or a raw Buffer. In the latter case,
* the result will be sent as a binary response.
*/
Connection.prototype.sendCommandResponse = function (id, response) {
this._send("commandResponse", {id: id, response: response });
if (Buffer.isBuffer(response)) {
// Assume the id is an unsigned 32-bit integer, which is encoded
// as a four-byte header
var header = new Buffer(4);

header.writeUInt32LE(id, 0);

// Prepend the header to the message
var message = Buffer.concat([header, response], response.length + 4);

this._sendBinary(message);
} else {
this._send("commandResponse", {id: id, response: response });
}
};

/**
* Sends a response indicating that an errur occurred during command
* Sends a response indicating that an error occurred during command
* execution
* @param {number} id unique ID of the command that was executed. ID is
* generated by the client when the command is issued.
Expand Down