3 var Base = require('./base'),
4 util = require('util');
6 var Draft75 = function(request, url, options) {
7 Base.apply(this, arguments);
9 this.version = 'hixie-75';
11 this._headers.set('Upgrade', 'WebSocket');
12 this._headers.set('Connection', 'Upgrade');
13 this._headers.set('WebSocket-Origin', this._request.headers.origin);
14 this._headers.set('WebSocket-Location', this.url);
16 util.inherits(Draft75, Base);
20 if (this.readyState === 3) return false;
22 this.emit('close', new Base.CloseEvent(null, null));
26 parse: function(chunk) {
27 if (this.readyState > 1) return;
29 this._reader.put(chunk);
31 this._reader.eachByte(function(octet) {
34 switch (this._stage) {
36 this._body.push(octet);
37 this._sendHandshakeBody();
41 this._parseLeadingByte(octet);
45 this._length = (octet & 0x7F) + 128 * this._length;
47 if (this._closing && this._length === 0) {
50 else if ((octet & 0x80) !== 0x80) {
51 if (this._length === 0) {
64 message = new Buffer(this._buffer).toString('utf8', 0, this._buffer.length);
65 this.emit('message', new Base.MessageEvent(message));
70 if (this._skipped === this._length)
73 this._buffer.push(octet);
74 if (this._buffer.length > this._maxLength) return this.close();
82 frame: function(buffer) {
83 if (this.readyState === 0) return this._queue([buffer]);
84 if (this.readyState > 1) return false;
86 if (typeof buffer !== 'string') buffer = buffer.toString();
88 var payload = new Buffer(buffer, 'utf8'),
89 frame = new Buffer(payload.length + 2);
92 frame[payload.length + 1] = 0xFF;
93 payload.copy(frame, 1);
99 _handshakeResponse: function() {
100 var start = 'HTTP/1.1 101 Web Socket Protocol Handshake',
101 headers = [start, this._headers.toString(), ''];
103 return new Buffer(headers.join('\r\n'), 'utf8');
106 _parseLeadingByte: function(octet) {
107 if ((octet & 0x80) === 0x80) {
112 delete this._skipped;
119 for (var key in instance)
120 Draft75.prototype[key] = instance[key];
122 module.exports = Draft75;