Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

Commit

Permalink
Modified connection.send so that it uses buffers under the hood, shou…
Browse files Browse the repository at this point in the history
…ld allow for multibyte characters. Fixes #56
  • Loading branch information
miksago committed Apr 15, 2011
1 parent 7462cef commit 4536830
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions lib/ws/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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:
-----------------------------------------------*/
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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.');
}
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 4536830

Please sign in to comment.