forked from sitegui/nodejs-websocket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OutStream.js
59 lines (53 loc) · 1.62 KB
/
OutStream.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* @file Simple wrapper for stream.Writable, used for sending binary data
*/
'use strict'
var util = require('util'),
stream = require('stream'),
frame = require('./frame')
/**
* @class Represents the writable stream for binary frames
* @param {Connection} connection
* @param {number} minSize
*/
function OutStream(connection, minSize) {
var that = this
this.connection = connection
this.minSize = minSize
this.buffer = new Buffer(0)
this.hasSent = false // Indicates if any frame has been sent yet
stream.Writable.call(this)
this.on('finish', function () {
if (that.connection.readyState === that.connection.OPEN) {
// Ignore if not connected anymore
that.connection.socket.write(frame.createBinaryFrame(that.buffer, !that.connection.server, !that.hasSent, true))
}
that.connection.outStream = null
})
}
module.exports = OutStream
util.inherits(OutStream, stream.Writable)
/**
* @param {Buffer} chunk
* @param {string} encoding
* @param {Function} callback
* @private
*/
OutStream.prototype._write = function (chunk, encoding, callback) {
var frameBuffer
this.buffer = Buffer.concat([this.buffer, chunk], this.buffer.length + chunk.length)
if (this.buffer.length >= this.minSize) {
if (this.connection.readyState === this.connection.OPEN) {
// Ignore if not connected anymore
frameBuffer = frame.createBinaryFrame(this.buffer, !this.connection.server, !this.hasSent, false)
this.connection.socket.write(frameBuffer, encoding, callback)
}
this.buffer = new Buffer(0)
this.hasSent = true
if (this.connection.readyState !== this.connection.OPEN) {
callback()
}
} else {
callback()
}
}