Initial commit
[yaffs-website] / node_modules / websocket-driver / lib / websocket / driver / hybi / message.js
1 'use strict';
2
3 var Message = function() {
4   this.rsv1    = false;
5   this.rsv2    = false;
6   this.rsv3    = false;
7   this.opcode  = null
8   this.length  = 0;
9   this._chunks = [];
10 };
11
12 var instance = {
13   read: function() {
14     if (this.data) return this.data;
15
16     this.data  = new Buffer(this.length);
17     var offset = 0;
18
19     for (var i = 0, n = this._chunks.length; i < n; i++) {
20       this._chunks[i].copy(this.data, offset);
21       offset += this._chunks[i].length;
22     }
23     return this.data;
24   },
25
26   pushFrame: function(frame) {
27     this.rsv1 = this.rsv1 || frame.rsv1;
28     this.rsv2 = this.rsv2 || frame.rsv2;
29     this.rsv3 = this.rsv3 || frame.rsv3;
30
31     if (this.opcode === null) this.opcode = frame.opcode;
32
33     this._chunks.push(frame.payload);
34     this.length += frame.length;
35   }
36 };
37
38 for (var key in instance)
39   Message.prototype[key] = instance[key];
40
41 module.exports = Message;