From 21443f312522b82ae6ca128c4afc8ab1673bee6e Mon Sep 17 00:00:00 2001 From: Siarhei Buntsevich Date: Mon, 28 Jan 2019 17:31:21 +0300 Subject: [PATCH 1/2] cork client fix --- stream.js | 15 +++++++++++++-- test-client.js | 13 +++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/stream.js b/stream.js index d138eea..f0a65ee 100644 --- a/stream.js +++ b/stream.js @@ -73,8 +73,14 @@ function WebSocketStream(target, protocols, options) { if (socket.readyState === socket.OPEN) { stream = proxy } else { - stream = duplexify.obj() - socket.onopen = onopen + if (isBrowser) { + stream = proxy + stream.cork() + socket.onopen = onopenBrowser + } else { + stream = duplexify.obj() + socket.onopen = onopen + } } stream.socket = socket @@ -131,6 +137,11 @@ function WebSocketStream(target, protocols, options) { stream.emit('connect') } + function onopenBrowser () { + stream.uncork() + stream.emit('connect') + } + function onclose() { stream.end() stream.destroy() diff --git a/test-client.js b/test-client.js index b5ada2c..a2c75fd 100644 --- a/test-client.js +++ b/test-client.js @@ -59,3 +59,16 @@ test('coerce client data as binary', function(t) { }) stream.write('hello') }) +test('cork logic test', function (t) { + var stream = ws('ws://localhost:8343', { binary: true }) + stream.on('data', function(o) { + t.equal(o.toString(), 'hello', 'success!') + stream.destroy() + t.end() + }) + stream.cork() + stream.write('he') + stream.write('l') + stream.write('lo') + stream.uncork() +}) \ No newline at end of file From d1879ffc6a4ddc5759bc2b24f1b57f24ca9df72a Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 28 Jan 2019 18:45:34 +0300 Subject: [PATCH 2/2] fix if structure --- stream.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/stream.js b/stream.js index f0a65ee..9328589 100644 --- a/stream.js +++ b/stream.js @@ -72,15 +72,13 @@ function WebSocketStream(target, protocols, options) { // was already open when passed in if (socket.readyState === socket.OPEN) { stream = proxy + } else if (isBrowser) { + stream = proxy + stream.cork() + socket.onopen = onopenBrowser } else { - if (isBrowser) { - stream = proxy - stream.cork() - socket.onopen = onopenBrowser - } else { - stream = duplexify.obj() - socket.onopen = onopen - } + stream = duplexify.obj() + socket.onopen = onopen } stream.socket = socket