Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cork/uncork logic client fix #147

Merged
merged 2 commits into from
Mar 26, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
scarry1992 marked this conversation as resolved.
Show resolved Hide resolved
stream = proxy
stream.cork()
socket.onopen = onopenBrowser
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can the same logic be applied to Node as well? What will cause issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to use same logic for Node and browser, but for Node needed duplexify logic for destroy() method as I see.

} else {
stream = duplexify.obj()
socket.onopen = onopen
}
}

stream.socket = socket
Expand Down Expand Up @@ -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()
Expand Down
13 changes: 13 additions & 0 deletions test-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})