Skip to content

Commit

Permalink
[major] Use the same handler for binary and text messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lpinca committed Dec 20, 2016
1 parent fc956f8 commit 097f39f
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 37 deletions.
10 changes: 4 additions & 6 deletions lib/Receiver.js
Expand Up @@ -53,10 +53,9 @@ class Receiver {

this.dead = false;

this.onbinary = noop;
this.onmessage = noop;
this.onclose = noop;
this.onerror = noop;
this.ontext = noop;
this.onping = noop;
this.onpong = noop;

Expand Down Expand Up @@ -350,14 +349,14 @@ class Receiver {
this.fragmented = 0;

if (this.opcode === 2) {
this.onbinary(buf, { masked: this.masked });
this.onmessage(buf, { masked: this.masked, binary: true });
} else {
if (!Validation.isValidUTF8(buf)) {
this.error(new Error('invalid utf8 sequence'), 1007);
return;
}

this.ontext(buf.toString(), { masked: this.masked });
this.onmessage(buf.toString(), { masked: this.masked });
}
}

Expand Down Expand Up @@ -477,10 +476,9 @@ class Receiver {
this.buffers = null;
this.mask = null;

this.onbinary = null;
this.onmessage = null;
this.onclose = null;
this.onerror = null;
this.ontext = null;
this.onping = null;
this.onpong = null;
}
Expand Down
10 changes: 3 additions & 7 deletions lib/WebSocket.js
Expand Up @@ -136,11 +136,7 @@ class WebSocket extends EventEmitter {
});

// receiver event handlers
this._receiver.ontext = (data, flags) => this.emit('message', data, flags);
this._receiver.onbinary = (data, flags) => {
flags.binary = true;
this.emit('message', data, flags);
};
this._receiver.onmessage = (data, flags) => this.emit('message', data, flags);
this._receiver.onping = (data, flags) => {
this.pong(data, { mask: !this._isServer }, true);
this.emit('ping', data, flags);
Expand All @@ -151,9 +147,9 @@ class WebSocket extends EventEmitter {
this._closeCode = code;
this.close(code, reason);
};
this._receiver.onerror = (error, errorCode) => {
this._receiver.onerror = (error, code) => {
// close the connection when the receiver reports a HyBi error code
this.close(errorCode, '');
this.close(code, '');
this.emit('error', error);
};

Expand Down
34 changes: 17 additions & 17 deletions test/Receiver.test.js
Expand Up @@ -17,7 +17,7 @@ describe('Receiver', function () {
it('can parse unmasked text message', function (done) {
const p = new Receiver();

p.ontext = function (data) {
p.onmessage = function (data) {
assert.strictEqual(data, 'Hello');
done();
};
Expand All @@ -40,7 +40,7 @@ describe('Receiver', function () {
it('can parse masked text message', function (done) {
const p = new Receiver();

p.ontext = function (data) {
p.onmessage = function (data) {
assert.strictEqual(data, '5:::{"name":"echo"}');
done();
};
Expand All @@ -56,7 +56,7 @@ describe('Receiver', function () {
const frame = Buffer.from('81FE' + util.pack(4, msg.length) + mask +
util.mask(msg, mask).toString('hex'), 'hex');

p.ontext = function (data) {
p.onmessage = function (data) {
assert.strictEqual(data, msg);
done();
};
Expand All @@ -73,7 +73,7 @@ describe('Receiver', function () {
const frame = '81FF' + util.pack(16, msg.length) + mask +
util.mask(msg, mask).toString('hex');

p.ontext = function (data) {
p.onmessage = function (data) {
assert.strictEqual(data, msg);
done();
};
Expand All @@ -94,7 +94,7 @@ describe('Receiver', function () {
const frame2 = '80FE' + util.pack(4, fragment2.length) + mask +
util.mask(fragment2, mask).toString('hex');

p.ontext = function (data) {
p.onmessage = function (data) {
assert.strictEqual(data, msg);
done();
};
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('Receiver', function () {

let gotPing = false;

p.ontext = function (data) {
p.onmessage = function (data) {
assert.strictEqual(data, msg);
assert.ok(gotPing);
done();
Expand Down Expand Up @@ -187,7 +187,7 @@ describe('Receiver', function () {

let gotPing = false;

p.ontext = function (data) {
p.onmessage = function (data) {
assert.strictEqual(data, msg);
assert.ok(gotPing);
done();
Expand All @@ -210,8 +210,8 @@ describe('Receiver', function () {
const frame = '82' + util.getHybiLengthAsHexString(msg.length, true) + mask +
util.mask(msg, mask).toString('hex');

p.onbinary = function (data) {
assert.deepStrictEqual(data.toString('hex'), msg.toString('hex'));
p.onmessage = function (data) {
assert.ok(data.equals(msg));
done();
};

Expand All @@ -226,8 +226,8 @@ describe('Receiver', function () {
const frame = '82' + util.getHybiLengthAsHexString(msg.length, true) + mask +
util.mask(msg, mask).toString('hex');

p.onbinary = function (data) {
assert.deepStrictEqual(data, msg);
p.onmessage = function (data) {
assert.ok(data.equals(msg));
done();
};

Expand All @@ -242,8 +242,8 @@ describe('Receiver', function () {
const frame = '82' + util.getHybiLengthAsHexString(msg.length, true) + mask +
util.mask(msg, mask).toString('hex');

p.onbinary = function (data) {
assert.deepStrictEqual(data, msg);
p.onmessage = function (data) {
assert.ok(data.equals(msg));
done();
};

Expand All @@ -257,8 +257,8 @@ describe('Receiver', function () {
const frame = '82' + util.getHybiLengthAsHexString(msg.length, false) +
msg.toString('hex');

p.onbinary = function (data) {
assert.deepStrictEqual(data, msg);
p.onmessage = function (data) {
assert.ok(data.equals(msg));
done();
};

Expand All @@ -272,7 +272,7 @@ describe('Receiver', function () {
const p = new Receiver({ 'permessage-deflate': perMessageDeflate });
const buf = Buffer.from('Hello');

p.ontext = function (data) {
p.onmessage = function (data) {
assert.strictEqual(data, 'Hello');
done();
};
Expand All @@ -293,7 +293,7 @@ describe('Receiver', function () {
const buf1 = Buffer.from('foo');
const buf2 = Buffer.from('bar');

p.ontext = function (data) {
p.onmessage = function (data) {
assert.strictEqual(data, 'foobar');
done();
};
Expand Down
9 changes: 2 additions & 7 deletions test/testserver.js
Expand Up @@ -62,14 +62,9 @@ function validServer (server, req, socket) {

receiver.onping = (message, flags) => server.emit('ping', message, flags);
receiver.onpong = (message, flags) => server.emit('pong', message, flags);
receiver.ontext = (message, flags) => {
receiver.onmessage = (message, flags) => {
server.emit('message', message, flags);
sender.send(message, { fin: true });
};
receiver.onbinary = (message, flags) => {
flags.binary = true;
server.emit('message', message, flags);
sender.send(message, { binary: true, fin: true });
sender.send(message, { binary: flags.binary, fin: true });
};
receiver.onclose = (code, message, flags) => {
sender.close(code, message, false, () => {
Expand Down

0 comments on commit 097f39f

Please sign in to comment.