From 90357529aa4391eda166083596d28ef1acd1855b Mon Sep 17 00:00:00 2001 From: richie Date: Mon, 3 Jun 2019 21:50:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=89=8D=E7=AB=AF=20?= =?UTF-8?q?=E5=B1=95=E7=A4=BA=E6=95=B0=E6=8D=AE=E4=B9=B1=E7=A0=81=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- doc/demo/client/index.html | 40 ++++++++++---------------------------- pkg/api/controller.go | 2 +- 2 files changed, 11 insertions(+), 31 deletions(-) diff --git a/doc/demo/client/index.html b/doc/demo/client/index.html index 3af412b..784e9da 100644 --- a/doc/demo/client/index.html +++ b/doc/demo/client/index.html @@ -30,6 +30,7 @@ + class ConvertHelper { constructor() { this.buf = null; @@ -78,34 +79,24 @@ } else { this.isChunk = false; } - + // console.log(`buf.byteLength: ${buf.byteLength}`); + // console.log(buf); if (this.isChunk) { if (!this.isStartValid(buf)) { - buf = this.concat(this.buf, buf); - this.buf = null; + // buf = this.concat(this.buf, buf); + buf = this.getBufAll(buf); } // console.log(buf); // console.log(this.isStartValid(buf)); // console.log(this.isEndValid(buf)); // console.log(this.getLastInValidPos(buf)); - if (!this.isEndValid(buf)) { - var pos = this.getLastInValidPos(buf); - if (pos < 0) { - cb(null, this.forceUtf8Slice(buf.subarray(0, pos))); - this.buf = buf.subarray(pos); - } else { - cb(null, this.forceUtf8Slice(buf)); - this.buf = null; - } + this.bufList.push(buf); } else { - cb(null, this.forceUtf8Slice(buf)); - this.buf = null; + cb(null, this.forceUtf8Slice(this.getBufAll(buf))); } } else { - buf = this.concat(this.buf, buf); - this.buf = null; - cb(null, this.forceUtf8Slice(buf)); + cb(null, this.forceUtf8Slice(this.getBufAll(buf))); } } @@ -135,21 +126,10 @@ if (buf instanceof ArrayBuffer) { buf = new Uint8Array(buf); } - if (buf.byteLength >= 4 * 1024) { - this.isChunk = true; - } else { - this.isChunk = false; - } - - if (!this.isChunk) { + if (this.isEndValid(buf)) { cb(null, this.forceUtf8Slice(this.getBufAll(buf))); - this.bufList.length = 0; } else { - if (this.isEndValid(buf)) { - cb(null, this.forceUtf8Slice(this.getBufAll(buf))); - } else { - this.bufList.push(buf); - } + this.bufList.push(buf); } } diff --git a/pkg/api/controller.go b/pkg/api/controller.go index f2186b9..2f2d1cf 100644 --- a/pkg/api/controller.go +++ b/pkg/api/controller.go @@ -85,7 +85,7 @@ func (handler *streamHandler) Read(p []byte) (size int, err error) { // executor 回调想web 输出 func (handler *streamHandler) Write(p []byte) (size int, err error) { size = len(p) - copy := append(make([]byte, 0, size), p...) + copy := append(make([]byte, 0, size), p...) // 解决 发送数据丢失的问题 err = handler.wsConn.WsWrite(websocket.BinaryMessage, copy) return }