websocket-stream
Advanced tools
Comparing version 0.0.5 to 0.1.0
@@ -6,2 +6,3 @@ var WebSocketServer = require('ws').Server | ||
var server = http.createServer(ecstatic) | ||
var stringStream = require('stringstream') | ||
var websocketStream = require('./') | ||
@@ -12,3 +13,4 @@ server.listen(8080) | ||
var stream = websocketStream(ws) | ||
fs.createReadStream('package.json').pipe(stream) | ||
var pj = fs.createReadStream('package.json') | ||
pj.pipe(stringStream()).pipe(stream) | ||
console.log('started stream') | ||
@@ -15,0 +17,0 @@ stream.on('end', function() { |
@@ -5,2 +5,3 @@ var websocket = require('./') | ||
var elstream = elstreamo.writable('#messages') | ||
ws.on('data', function(c) { console.log(c) }) | ||
ws.pipe(elstream) |
28
index.js
var stream = require('stream') | ||
var util = require('util') | ||
var isBuffer = require('isbuffer') | ||
function WebsocketStream(server, protocol) { | ||
var self = this | ||
function WebsocketStream(server, options) { | ||
if (!(this instanceof WebsocketStream)) return new WebsocketStream(server, options) | ||
stream.Stream.call(this) | ||
this.options = options || {} | ||
this.readable = true | ||
@@ -14,10 +16,10 @@ this.writable = true | ||
this.ws.on('close', this.onClose.bind(this)) | ||
this.ws.on('open', function() { | ||
self.emit('open') | ||
}) | ||
this.ws.on('open', this.onOpen.bind(this)) | ||
} else { | ||
this.ws = new WebSocket(server, protocol) | ||
this.ws = new WebSocket(server, this.options.protocol) | ||
this.ws.binaryType = this.options.binaryType || 'arraybuffer' | ||
this.ws.onmessage = this.onMessage.bind(this) | ||
this.ws.onerror = this.onError.bind(this) | ||
this.ws.onclose = this.onClose.bind(this) | ||
this.ws.onopen = this.onOpen.bind(this) | ||
} | ||
@@ -28,6 +30,3 @@ } | ||
module.exports = function(server, protocol) { | ||
return new WebsocketStream(server, protocol) | ||
} | ||
module.exports = WebsocketStream | ||
module.exports.WebsocketStream = WebsocketStream | ||
@@ -48,4 +47,11 @@ | ||
WebsocketStream.prototype.onOpen = function(err) { | ||
this.emit('open') | ||
this.emit('connect') | ||
} | ||
WebsocketStream.prototype.write = function(data) { | ||
return this.ws.send(data) | ||
typeof WebSocket != 'undefined' && this.ws instanceof WebSocket | ||
? this.ws.send(data) | ||
: this.ws.send(data, { binary : isBuffer(data) }) | ||
} | ||
@@ -52,0 +58,0 @@ |
{ | ||
"name": "websocket-stream", | ||
"description": "websockets with the node stream api. works in browser and node", | ||
"keywords": ["websocket", "websockets", "stream", "streams", "realtime"], | ||
"version": "0.0.5", | ||
"scripts": { | ||
"start": "browserify demo.js -o demo-bundle.js && node demo-server.js" | ||
}, | ||
"keywords": [ | ||
"websocket", | ||
"websockets", | ||
"stream", | ||
"streams", | ||
"realtime" | ||
], | ||
"version": "0.1.0", | ||
"_npmUser": { | ||
@@ -10,7 +19,11 @@ "name": "maxogden", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"isbuffer": "0.0.0" | ||
}, | ||
"devDependencies": { | ||
"ws": "0.4.22", | ||
"el-streamo": "1.0.0", | ||
"ecstatic": "0.1.7" | ||
"ecstatic": "0.4.2", | ||
"browserify": "2.14.1", | ||
"stringstream": "0.0.4" | ||
}, | ||
@@ -17,0 +30,0 @@ "optionalDependencies": {}, |
@@ -9,3 +9,3 @@ # websocket-stream | ||
you can use [browserify](http://github.com/substack/node-browserify) to package this module for browser use. there is a also pre-made + minified version you can download and use right away called `websocket-stream-min.js` | ||
you can use [browserify](http://github.com/substack/node-browserify) to package this module for browser use. | ||
@@ -34,8 +34,28 @@ ```javascript | ||
## extras | ||
## options | ||
you can pass in a custom protocol to the constructor as the second argument | ||
pass in options as the second argument like this: | ||
`require('websocket-stream').WebsocketStream` is the raw constructor | ||
```js | ||
websocketStream('ws://foobar', { binaryType: 'blob' }) | ||
``` | ||
BSD LICENSE | ||
possible options are... | ||
```js | ||
{ | ||
protocol: // optional, string, specify websocket protocol | ||
binaryType: // optional, string, defaults to 'arraybuffer', can also be 'blob' | ||
} | ||
``` | ||
### binary sockets | ||
To send binary data just write a [Buffer](nodejs.org/api/buffer.html) or [TypedArray](https://developer.mozilla.org/en-US/docs/JavaScript/Typed_arrays) to the stream. | ||
On the other end you will receive [Buffer](nodejs.org/api/buffer.html) instances if it's the server and [ArrayBuffer](https://developer.mozilla.org/en-US/docs/JavaScript/Typed_arrays/ArrayBuffer) instances if it's the client. The client will default to `ArrayBuffer` objects but can also be configured to receive `Blob`s. | ||
If you write binary data to a websocket on the server, the client will receive binary objects. Same thing goes for strings. | ||
## license | ||
BSD LICENSE |
Sorry, the diff of this file is too big to display
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Debug access
Supply chain riskUses debug, reflection and dynamic code execution features.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
85147
9
2455
2
60
6
1
5
2
+ Addedisbuffer@0.0.0
+ Addedisbuffer@0.0.0(transitive)