Skip to content

Commit 7460782

Browse files
authored
Merge pull request #4527 from jerch/fix_4485
fix buffer corruption for utf8 transport in demo
2 parents cdec1e5 + 048010c commit 7460782

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

demo/server.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -111,35 +111,32 @@ function startServer() {
111111
}
112112
// binary message buffering
113113
function bufferUtf8(socket, timeout, maxSize) {
114-
const dataBuffer = new Uint8Array(maxSize);
115-
let sender = null;
114+
const chunks = [];
116115
let length = 0;
116+
let sender = null;
117117
return (data) => {
118-
function flush() {
119-
socket.send(Buffer.from(dataBuffer.buffer, 0, length));
118+
chunks.push(data);
119+
length += data.length;
120+
if (length > maxSize || userInput) {
121+
userInput = false;
122+
socket.send(Buffer.concat(chunks));
123+
chunks.length = 0;
120124
length = 0;
121125
if (sender) {
122126
clearTimeout(sender);
123127
sender = null;
124128
}
125-
}
126-
if (length + data.length > maxSize) {
127-
flush();
128-
}
129-
dataBuffer.set(data, length);
130-
length += data.length;
131-
if (length > maxSize || userInput) {
132-
userInput = false;
133-
flush();
134129
} else if (!sender) {
135130
sender = setTimeout(() => {
131+
socket.send(Buffer.concat(chunks));
132+
chunks.length = 0;
133+
length = 0;
136134
sender = null;
137-
flush();
138135
}, timeout);
139136
}
140137
};
141138
}
142-
const send = (USE_BINARY ? bufferUtf8 : buffer)(ws, 5, 262144);
139+
const send = (USE_BINARY ? bufferUtf8 : buffer)(ws, 3, 262144);
143140

144141
// WARNING: This is a naive implementation that will not throttle the flow of data. This means
145142
// it could flood the communication channel and make the terminal unresponsive. Learn more about

0 commit comments

Comments
 (0)