diff --git a/lib/ws/connection.js b/lib/ws/connection.js index 65dcbde..62de57f 100644 --- a/lib/ws/connection.js +++ b/lib/ws/connection.js @@ -13,6 +13,10 @@ var util = require('../_util'), var _events = require('../_events'); var Mixin = require('../lang/mixin'); +var CLOSE_FRAME = new Buffer(2); + CLOSE_FRAME[0] = 0xFF; + CLOSE_FRAME[1] = 0x00; + /*----------------------------------------------- The Connection: -----------------------------------------------*/ @@ -183,9 +187,9 @@ util.inherits(Connection, _events.EventEmitter); Various utility style functions: -----------------------------------------------*/ function write(connection, data) { - debug(connection.id, 'write: ', (new Buffer(data)).inspect()); + debug(connection.id, 'write: ', data.inspect()); if (connection._socket.writable) { - return connection._socket.write(data, 'binary'); + return connection._socket.write(data); } return false; } @@ -282,7 +286,14 @@ Connection.prototype.inspect = function() { Connection.prototype.write = function(data) { if (this._state === 4) { - return write(this, '\u0000' + data + '\uffff'); + var byteLen = Buffer.byteLength(data, 'utf8'), + bytes = new Buffer(byteLen+2); + + bytes[0] = 0x00; + bytes.write(data, 1, 'utf8'); + bytes[byteLen + 1] = 0xFF; + + return write(this, bytes); } else { debug(this.id, '\033[31mCould not send.'); } @@ -303,7 +314,7 @@ Connection.prototype.close = function() { var connection = this; if (connection._state == 4 && connection._socket.writable) { - write(connection, '\xff\x00'); + write(connection, CLOSE_FRAME); } // Add a two second timeout for closing connections. @@ -423,7 +434,7 @@ util.inherits(Parser, events.EventEmitter); Parser.prototype.write = function(data) { var pkt, msg; - debug('parse.write', (new Buffer(data)).inspect()); + debug('parse.write', data.inspect()); for (var i = 0, len = data.length; i < len; i++) { if (this.order == 0) {