先是
return createServer()
.then(function(wss) {
var frameProducer = new FrameProducer(
new FrameConfig(display.properties, display.properties))
var broadcastSet = frameProducer.broadcastSet = new BroadcastSet()
然后
ws.on('message', function(data) {
var match = /^(on|off|(size) ([0-9]+)x([0-9]+))$/.exec(data)
if (match) {
switch (match[2] || match[1]) {
case 'on':
broadcastSet.insert(id, {
onStart: wsStartNotifier
, onFrame: wsFrameNotifier
})
break
再然后
BroadcastSet.prototype.insert = function(id, ws) {
if (!(id in this.set)) {
this.set[id] = ws
this.count += 1
this.emit('insert', id)
if (this.count === 1) {
this.emit('nonempty')
}
}
}
再次
broadcastSet.on('nonempty', function() {
frameProducer.start()
})
然后
FrameProducer.prototype.start = function() {
log.info('Requesting frame producer to start')
this.desiredState.push(FrameProducer.STATE_STARTED)
this._ensureState()
}
然后
.then(function(banner) {
this.banner = banner
return this._readFrames(this.socket.stream)
})
.then(function() {
this.runningState = FrameProducer.STATE_STARTED
this.emit('start')
})
然后
FrameProducer.prototype._readFrames = function(socket) {
this.needsReadable = true
socket.on('readable', this.readableListener)
// We may already have data pending. Let the user know they should
// at least attempt to read frames now.
this.readableListener()
}
与
frameProducer.on('start', function() {
broadcastSet.keys().map(function(id) {
return broadcastSet.get(id).onStart(frameProducer)
})
})
然后
this.readableListener = this._readableListener.bind(this)
然后
FrameProducer.prototype._readableListener = function() {
this.readable = true
this._maybeEmitReadable()
}
然后
FrameProducer.prototype._maybeEmitReadable = function() {
if (this.readable && this.needsReadable) {
this.needsReadable = false
this.emit('readable')
}
}
然后
frameProducer.on('readable', function next() {
var frame = frameProducer.nextFrame()
if (frame) {
Promise.settle([broadcastSet.keys().map(function(id) {
return broadcastSet.get(id).onFrame(frame)
})]).then(next)
}
else {
frameProducer.needFrame()
}
})
然后
FrameProducer.prototype.nextFrame = function() {
var frame = null
var chunk
if (this.parser) {
while ((frame = this.parser.nextFrame()) === null) {
chunk = this.socket.stream.read()
if (chunk) {
this.parser.push(chunk)
}
else {
this.readable = false
break
}
}
}
return frame
}
再然后
function wsFrameNotifier(frame) {
return send(frame, {
binary: true
})
}