|
1 | | -var through = require('through') |
2 | | -var isBuffer = require('isbuffer') |
3 | | -var WebSocketPoly = require('ws') |
| 1 | +var through = require('through2') |
| 2 | +var duplexify = require('duplexify') |
| 3 | +var WS = require('ws') |
4 | 4 |
|
5 | | -function WebsocketStream(server, options) { |
6 | | - if (!(this instanceof WebsocketStream)) return new WebsocketStream(server, options) |
| 5 | +module.exports = WebSocketStream |
7 | 6 |
|
8 | | - this.stream = through(this.write.bind(this), this.end.bind(this)) |
9 | | - |
10 | | - this.stream.websocketStream = this |
11 | | - this.options = options || {} |
12 | | - this._buffer = [] |
13 | | - |
14 | | - if (typeof server === "object") { |
15 | | - this.ws = server |
16 | | - this.ws.on('message', this.onMessage.bind(this)) |
17 | | - this.ws.on('error', this.onError.bind(this)) |
18 | | - this.ws.on('close', this.onClose.bind(this)) |
19 | | - this.ws.on('open', this.onOpen.bind(this)) |
20 | | - if (this.ws.readyState === 1) this._open = true |
| 7 | +function WebSocketStream(target, options) { |
| 8 | + if (!options) options = {} |
| 9 | + var stream, socket |
| 10 | + var proxy = through(socketWrite, socketEnd) |
| 11 | + |
| 12 | + // use existing WebSocket object that was passed in |
| 13 | + if (typeof target === 'object') { |
| 14 | + socket = target |
| 15 | + // otherwise make a new one |
21 | 16 | } else { |
22 | | - var opts = (process.title === 'browser') ? this.options.protocol : this.options |
23 | | - this.ws = new WebSocketPoly(server, opts) |
24 | | - this.ws.binaryType = this.options.binaryType || 'arraybuffer' |
25 | | - this.ws.onmessage = this.onMessage.bind(this) |
26 | | - this.ws.onerror = this.onError.bind(this) |
27 | | - this.ws.onclose = this.onClose.bind(this) |
28 | | - this.ws.onopen = this.onOpen.bind(this) |
| 17 | + socket = new WS(target, options) |
| 18 | + socket.binaryType = 'arraybuffer' |
| 19 | + } |
| 20 | + |
| 21 | + // was already open when passed in |
| 22 | + if (socket.readyState === 1) { |
| 23 | + stream = proxy |
| 24 | + } else { |
| 25 | + stream = duplexify() |
| 26 | + socket.addEventListener("open", onready) |
| 27 | + } |
| 28 | + |
| 29 | + socket.addEventListener("close", onclose) |
| 30 | + socket.addEventListener("error", onerror) |
| 31 | + socket.addEventListener("message", onmessage) |
| 32 | + |
| 33 | + function socketWrite(chunk, enc, next) { |
| 34 | + socket.send(chunk) |
| 35 | + next() |
29 | 36 | } |
30 | | - |
31 | | - this.stream.destroy = this.destroy.bind(this) |
32 | | - return this.stream |
33 | | -} |
34 | | - |
35 | | -module.exports = WebsocketStream |
36 | | -module.exports.WebsocketStream = WebsocketStream |
37 | | - |
38 | | -WebsocketStream.prototype.destroy = function() { |
39 | | - this.ws.close() |
40 | | -} |
41 | | - |
42 | | -WebsocketStream.prototype.onMessage = function(e) { |
43 | | - var data = e |
44 | | - if (typeof data.data !== 'undefined') data = data.data |
45 | | - |
46 | | - // type must be a Typed Array (ArrayBufferView) |
47 | | - var type = this.options.type |
48 | | - if (type && data instanceof ArrayBuffer) data = new type(data) |
49 | 37 |
|
50 | | - this.stream.queue(data) |
51 | | -} |
52 | | - |
53 | | -WebsocketStream.prototype.onError = function(err) { |
54 | | - this.stream.emit('error', err) |
55 | | -} |
56 | | - |
57 | | -WebsocketStream.prototype.onClose = function(err) { |
58 | | - if (this._destroy) return |
59 | | - this.stream.emit('end') |
60 | | - this.stream.emit('close') |
61 | | -} |
62 | | - |
63 | | -WebsocketStream.prototype.onOpen = function(err) { |
64 | | - if (this._destroy) return |
65 | | - this._open = true |
66 | | - for (var i = 0; i < this._buffer.length; i++) { |
67 | | - this._write(this._buffer[i]) |
| 38 | + function socketEnd(done) { |
| 39 | + socket.close() |
| 40 | + done() |
68 | 41 | } |
69 | | - this._buffer = undefined |
70 | | - this.stream.emit('open') |
71 | | - this.stream.emit('connect') |
72 | | - if (this._end) this.ws.close() |
73 | | -} |
74 | | - |
75 | | -WebsocketStream.prototype.write = function(data) { |
76 | | - if (!this._open) { |
77 | | - this._buffer.push(data) |
78 | | - } else { |
79 | | - this._write(data) |
| 42 | + |
| 43 | + function onready() { |
| 44 | + stream.setReadable(proxy) |
| 45 | + stream.setWritable(proxy) |
80 | 46 | } |
81 | | -} |
82 | | - |
83 | | -WebsocketStream.prototype._write = function(data) { |
84 | | - if (this.ws.readyState == 1) |
85 | | - // we are connected |
86 | | - typeof WebSocket != 'undefined' && this.ws instanceof WebSocket |
87 | | - ? this.ws.send(data) |
88 | | - : this.ws.send(data, { binary : isBuffer(data) }) |
89 | | - else |
90 | | - this.stream.emit('error', 'Not connected') |
91 | | -} |
92 | | - |
93 | | -WebsocketStream.prototype.end = function(data) { |
94 | | - if (data !== undefined) this.stream.queue(data) |
95 | | - if (this._open) this.ws.close() |
96 | | - this._end = true |
| 47 | + |
| 48 | + function onclose() { |
| 49 | + stream.destroy() |
| 50 | + } |
| 51 | + |
| 52 | + function onerror(err) { |
| 53 | + stream.destroy(err) |
| 54 | + } |
| 55 | + |
| 56 | + function onmessage(event) { |
| 57 | + var data = event.data |
| 58 | + if (data instanceof ArrayBuffer) data = new Buffer(new Uint8Array(data)) |
| 59 | + proxy.push(data) |
| 60 | + } |
| 61 | + |
| 62 | + return stream |
97 | 63 | } |
0 commit comments