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

feat: add properties and functions to client and server #1

Merged
Show file tree
Hide file tree
Changes from 2 commits
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ await server.listen(PORT)

Create a websocket client connection. Set `binary: true` to get a stream of arrayBuffers (on the browser). Defaults to true on node, but to strings on the browser. This may cause a problems if your application assumes binary.

For adding options to the Websocket instance, as [websockets/ws/blob/master/doc/ws.md#new-websocketaddress-protocols-options](https://github.com/websockets/ws/blob/master/doc/ws.md#new-websocketaddress-protocols-options), you can provide an object with the `websocket` property into the connect options.

```js
const stream = connect(url)
// stream is duplex and is both a `source` and `sink`.
Expand Down
4 changes: 3 additions & 1 deletion client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ var wsurl = require('./ws-url')

module.exports = function (addr, opts) {
const location = typeof window === 'undefined' ? {} : window.location
opts = opts || {}

Choose a reason for hiding this comment

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

I'd move this to a default param instead of checking here. module.exports = function (addr, opts = {}) {


const url = wsurl(addr, location)
const socket = new WebSocket(url)
const socket = new WebSocket(url, opts.websocket)

const stream = duplex(socket, opts)
stream.remoteAddress = url
stream.socket = socket

Choose a reason for hiding this comment

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

Is this needed? Can we map stream.destroy to socket.terminate? This would give users the forceful close option.

stream.close = () => new Promise((resolve, reject) => {
socket.addEventListener('close', resolve)
socket.close()
Expand Down
Loading