websocket-stream
Advanced tools
Comparing version 0.2.0 to 0.3.0
@@ -13,2 +13,4 @@ var WebSocketServer = require('ws').Server | ||
stream.end() | ||
}) | ||
}) | ||
console.log(':8080') |
@@ -18,2 +18,4 @@ var WebSocketServer = require('ws').Server | ||
}) | ||
}) | ||
}) | ||
console.log(':8080') |
var websocket = require('./') | ||
var elstreamo = require('el-streamo') | ||
ws = websocket('ws://localhost:8080') | ||
ws = websocket('ws://localhost:8080', {type: Uint8Array}) | ||
var elstream = elstreamo.writable('#messages') | ||
ws.on('data', function(c) { console.log(c) }) | ||
ws.on('data', function(c) { console.log({foo: c}) }) | ||
ws.pipe(elstream) |
11
index.js
@@ -22,3 +22,2 @@ var stream = require('stream') | ||
this.ws = new WebSocket(server, this.options.protocol) | ||
this.ws.binaryType = this.options.binaryType || 'arraybuffer' | ||
this.ws.onmessage = this.onMessage.bind(this) | ||
@@ -37,4 +36,10 @@ this.ws.onerror = this.onError.bind(this) | ||
WebsocketStream.prototype.onMessage = function(e, flags) { | ||
if (e.data) return this.emit('data', e.data, flags) | ||
this.emit('data', e, flags) | ||
var data = e | ||
if (data.data) data = data.data | ||
// type must be a Typed Array (ArrayBufferView) | ||
var type = this.options.type | ||
if (type && data instanceof ArrayBuffer) data = new type(data) | ||
this.emit('data', data, flags) | ||
} | ||
@@ -41,0 +46,0 @@ |
{ | ||
"name": "websocket-stream", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "websockets with the node stream api. works in browser and node", | ||
@@ -5,0 +5,0 @@ "scripts": { |
@@ -46,3 +46,5 @@ # websocket-stream | ||
```js | ||
websocketStream('ws://foobar', { binaryType: 'blob' }) | ||
websocketStream('ws://foobar', { type: someTypedArray }) | ||
// e.g. {type: Uint8Array} means you'll get Uint8Arrays back instead of ArrayBuffers | ||
``` | ||
@@ -55,3 +57,3 @@ | ||
protocol: // optional, string, specify websocket protocol | ||
binaryType: // optional, string, defaults to 'arraybuffer', can also be 'blob' | ||
type: // optional, TypedArray object, wraps the ArrayBuffer before emitting | ||
} | ||
@@ -58,0 +60,0 @@ ``` |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
7025
113
70
2