diff --git a/core/websock.js b/core/websock.js index 2b9b519a..711ea02b 100644 --- a/core/websock.js +++ b/core/websock.js @@ -177,7 +177,7 @@ export default class Websock { flush() { if (this._sQlen > 0 && this.readyState === 'open') { - this._websocket.send(this._encodeMessage()); + this._websocket.send(new Uint8Array(this._sQ.buffer, 0, this._sQlen)); this._sQlen = 0; } } @@ -268,11 +268,6 @@ export default class Websock { } // private methods - _encodeMessage() { - // Put in a binary arraybuffer - // according to the spec, you can send ArrayBufferViews with the send method - return new Uint8Array(this._sQ.buffer, 0, this._sQlen); - } // We want to move all the unread data to the start of the queue, // e.g. compacting. @@ -312,17 +307,14 @@ export default class Websock { } // push arraybuffer values onto the end of the receive que - _DecodeMessage(data) { - const u8 = new Uint8Array(data); + _recvMessage(e) { + const u8 = new Uint8Array(e.data); if (u8.length > this._rQbufferSize - this._rQlen) { this._expandCompactRQ(u8.length); } this._rQ.set(u8, this._rQlen); this._rQlen += u8.length; - } - _recvMessage(e) { - this._DecodeMessage(e.data); if (this._rQlen - this._rQi > 0) { this._eventHandlers.message(); if (this._rQlen == this._rQi) { diff --git a/tests/test.websock.js b/tests/test.websock.js index e72ba272..de7fe0d1 100644 --- a/tests/test.websock.js +++ b/tests/test.websock.js @@ -161,10 +161,9 @@ describe('Websock', function () { it('should actually send on the websocket', function () { sock._sQ = new Uint8Array([1, 2, 3]); sock._sQlen = 3; - const encoded = sock._encodeMessage(); sock.flush(); - expect(sock).to.have.sent(encoded); + expect(sock).to.have.sent(new Uint8Array([1, 2, 3])); }); it('should not call send if we do not have anything queued up', function () { @@ -397,9 +396,8 @@ describe('Websock', function () { sock._allocateBuffers(); }); - it('should support adding binary Uint8Array data to the receive queue', function () { + it('should support adding data to the receive queue', function () { const msg = { data: new Uint8Array([1, 2, 3]) }; - sock._mode = 'binary'; sock._recvMessage(msg); expect(sock.rQshiftStr(3)).to.equal('\x01\x02\x03'); }); @@ -426,7 +424,6 @@ describe('Websock', function () { sock._rQlen = 6; sock._rQi = 6; const msg = { data: new Uint8Array([1, 2, 3]).buffer }; - sock._mode = 'binary'; sock._recvMessage(msg); expect(sock._rQlen).to.equal(0); expect(sock._rQi).to.equal(0); @@ -438,7 +435,6 @@ describe('Websock', function () { sock._rQlen = 20; sock._rQi = 10; const msg = { data: new Uint8Array([1, 2]).buffer }; - sock._mode = 'binary'; sock._recvMessage(msg); expect(sock._rQlen).to.equal(12); expect(sock._rQi).to.equal(0); @@ -450,7 +446,6 @@ describe('Websock', function () { sock._rQi = 0; sock._rQbufferSize = 20; const msg = { data: new Uint8Array(30).buffer }; - sock._mode = 'binary'; sock._recvMessage(msg); expect(sock._rQlen).to.equal(30); expect(sock._rQi).to.equal(0); @@ -463,37 +458,10 @@ describe('Websock', function () { sock._rQi = 16; sock._rQbufferSize = 20; const msg = { data: new Uint8Array(6).buffer }; - sock._mode = 'binary'; sock._recvMessage(msg); expect(sock._rQlen).to.equal(6); expect(sock._rQi).to.equal(0); expect(sock._rQ.length).to.equal(48); }); }); - - describe('Data encoding', function () { - before(function () { FakeWebSocket.replace(); }); - after(function () { FakeWebSocket.restore(); }); - - describe('as binary data', function () { - let sock; - beforeEach(function () { - sock = new Websock(); - sock.open('ws://', 'binary'); - sock._websocket._open(); - }); - - it('should only send the send queue up to the send queue length', function () { - sock._sQ = new Uint8Array([1, 2, 3, 4, 5]); - sock._sQlen = 3; - const res = sock._encodeMessage(); - expect(res).to.array.equal(new Uint8Array([1, 2, 3])); - }); - - it('should properly pass the encoded data off to the actual WebSocket', function () { - sock.send([1, 2, 3]); - expect(sock._websocket._getSentData()).to.array.equal(new Uint8Array([1, 2, 3])); - }); - }); - }); });