Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

update receivers to avoid eventemitter

  • Loading branch information...
commit 8d5a6d9004ae5b53866d3e6dee0040171798ebbd 1 parent 9892d7b
Einar Otto Stangvik einaros authored
23 lib/Receiver.hixie.js
View
@@ -4,9 +4,7 @@
* MIT Licensed
*/
-var events = require('events')
- , util = require('util')
- , EventEmitter = events.EventEmitter;
+var util = require('util');
/**
* State constants
@@ -25,17 +23,18 @@ function Receiver () {
this.messageEnd = -1;
this.spanLength = 0;
this.dead = false;
+
+ this.onerror = function() {};
+ this.ontext = function() {};
+ this.onbinary = function() {};
+ this.onclose = function() {};
+ this.onping = function() {};
+ this.onpong = function() {};
}
module.exports = Receiver;
/**
- * Inherits from EventEmitter.
- */
-
-util.inherits(Receiver, events.EventEmitter);
-
-/**
* Add new data to the parser.
*
* @api public
@@ -47,7 +46,7 @@ Receiver.prototype.add = function(data) {
if (self.state === EMPTY) {
if (data.length == 2 && data[0] == 0xFF && data[1] == 0x00) {
self.reset();
- self.emit('close');
+ self.onclose();
return;
}
if (data[0] !== 0x00) {
@@ -100,7 +99,7 @@ Receiver.prototype.parse = function() {
tail = lastBuffer.slice(this.messageEnd + 1);
}
this.reset();
- this.emit('text', output.toString('utf8'));
+ this.ontext(output.toString('utf8'));
return tail;
}
@@ -112,7 +111,7 @@ Receiver.prototype.parse = function() {
Receiver.prototype.error = function (reason, terminate) {
this.reset();
- this.emit('error', reason, terminate);
+ this.onerror(reason, terminate);
return this;
}
35 lib/Receiver.js
View
@@ -4,9 +4,7 @@
* MIT Licensed
*/
-var events = require('events')
- , util = require('util')
- , EventEmitter = events.EventEmitter
+var util = require('util')
, Validation = require('./Validation').Validation
, ErrorCodes = require('./ErrorCodes')
, BufferPool = require('./BufferPool')
@@ -58,17 +56,18 @@ function Receiver () {
this.currentMessage = [];
this.expectHeader(2, this.processPacket);
this.dead = false;
+
+ this.onerror = function() {};
+ this.ontext = function() {};
+ this.onbinary = function() {};
+ this.onclose = function() {};
+ this.onping = function() {};
+ this.onpong = function() {};
};
module.exports = Receiver;
/**
- * Inherits from EventEmitter.
- */
-
-util.inherits(Receiver, events.EventEmitter);
-
-/**
* Add new data to the parser.
*
* @api public
@@ -111,6 +110,12 @@ Receiver.prototype.cleanup = function() {
this.fragmentedBufferPool = null;
this.state = null;
this.currentMessage = null;
+ this.onerror = null;
+ this.ontext = null;
+ this.onbinary = null;
+ this.onclose = null;
+ this.onping = null;
+ this.onpong = null;
}
/**
@@ -293,7 +298,7 @@ Receiver.prototype.concatBuffers = function(buffers) {
Receiver.prototype.error = function (reason, protocolErrorCode) {
this.reset();
- this.emit('error', reason, protocolErrorCode);
+ this.onerror(reason, protocolErrorCode);
return this;
}
@@ -389,7 +394,7 @@ var opcodes = {
this.error('invalid utf8 sequence', 1007);
return;
}
- this.emit('text', messageBuffer.toString('utf8'), {masked: this.state.masked, buffer: messageBuffer});
+ this.ontext(messageBuffer.toString('utf8'), {masked: this.state.masked, buffer: messageBuffer});
this.currentMessage = [];
}
this.endPacket();
@@ -440,7 +445,7 @@ var opcodes = {
if (packet != null) this.currentMessage.push(packet);
if (this.state.lastFragment) {
var messageBuffer = this.concatBuffers(this.currentMessage);
- this.emit('binary', messageBuffer, {masked: this.state.masked, buffer: messageBuffer});
+ this.onbinary(messageBuffer, {masked: this.state.masked, buffer: messageBuffer});
this.currentMessage = [];
}
this.endPacket();
@@ -501,7 +506,7 @@ var opcodes = {
}
message = messageBuffer.toString('utf8');
}
- this.emit('close', code, message, {masked: self.state.masked});
+ this.onclose(code, message, {masked: self.state.masked});
this.reset();
},
},
@@ -540,7 +545,7 @@ var opcodes = {
}
},
finish: function(mask, data) {
- this.emit('ping', this.unmask(mask, data, true), {masked: this.state.masked, binary: true});
+ this.onping(this.unmask(mask, data, true), {masked: this.state.masked, binary: true});
this.endPacket();
}
},
@@ -579,7 +584,7 @@ var opcodes = {
}
},
finish: function(mask, data) {
- this.emit('pong', this.unmask(mask, data, true), {masked: this.state.masked, binary: true});
+ this.onpong(this.unmask(mask, data, true), {masked: this.state.masked, binary: true});
this.endPacket();
}
}
25 lib/WebSocket.js
View
@@ -509,32 +509,32 @@ function establishConnection(ReceiverClass, SenderClass, socket, upgradeHead) {
process.nextTick(firstHandler);
// receiver event handlers
- self._receiver.on('text', function (data, flags) {
+ self._receiver.ontext = function (data, flags) {
flags = flags || {};
self.emit('message', data, flags);
- });
- self._receiver.on('binary', function (data, flags) {
+ };
+ self._receiver.onbinary = function (data, flags) {
flags = flags || {};
flags.binary = true;
self.emit('message', data, flags);
- });
- self._receiver.on('ping', function(data, flags) {
+ };
+ self._receiver.onping = function(data, flags) {
flags = flags || {};
self.pong(data, {mask: !self._isServer, binary: flags.binary === true}, true);
self.emit('ping', data, flags);
- });
- self._receiver.on('pong', function(data, flags) {
+ };
+ self._receiver.onpong = function(data, flags) {
self.emit('pong', data, flags);
- });
- self._receiver.on('close', function(code, data, flags) {
+ };
+ self._receiver.onclose = function(code, data, flags) {
flags = flags || {};
self.close(code, data);
- });
- self._receiver.on('error', function(reason, errorCode) {
+ };
+ self._receiver.onerror = function(reason, errorCode) {
// close the connection when the receiver reports a HyBi error code
self.close(typeof errorCode != 'undefined' ? errorCode : 1002, '');
self.emit('error', reason, errorCode);
- });
+ };
// finalize the client
this._sender = new SenderClass(socket);
@@ -610,7 +610,6 @@ function cleanupWebsocketResources(error) {
this._sender = null;
}
if (this._receiver) {
- removeAllListeners(this._receiver);
this._receiver.cleanup();
this._receiver = null;
}
4 lib/WebSocketServer.js
View
@@ -196,6 +196,8 @@ function handleHybiUpgrade(req, socket, upgradeHead, cb) {
headers['Sec-WebSocket-Protocol'] = protocol;
}
+ socket.setTimeout(0);
+ socket.setNoDelay(true);
try {
socket.write(headers.concat('', '').join('\r\n'));
}
@@ -205,8 +207,6 @@ function handleHybiUpgrade(req, socket, upgradeHead, cb) {
return;
}
- socket.setTimeout(0);
- socket.setNoDelay(true);
var client = new WebSocket([req, socket, upgradeHead], {
protocolVersion: version,
protocol: protocol
32 test/Receiver.hixie.test.js
View
@@ -9,10 +9,10 @@ describe('Receiver', function() {
var packet = '00 48 65 6c 6c 6f ff';
var gotData = false;
- p.on('text', function(data) {
+ p.ontext = function(data) {
gotData = true;
assert.equal('Hello', data);
- });
+ };
p.add(getBufferFromHexString(packet));
expect(gotData).to.equal(true);
@@ -24,10 +24,10 @@ describe('Receiver', function() {
var gotData = false;
var messages = [];
- p.on('text', function(data) {
+ p.ontext = function(data) {
gotData = true;
messages.push(data);
- });
+ };
p.add(getBufferFromHexString(packet));
expect(gotData).to.equal(true);
@@ -41,10 +41,10 @@ describe('Receiver', function() {
var packet = '00 ff';
var gotData = false;
- p.on('text', function(data) {
+ p.ontext = function(data) {
gotData = true;
assert.equal('', data);
- });
+ };
p.add(getBufferFromHexString(packet));
expect(gotData).to.equal(true);
@@ -63,10 +63,10 @@ describe('Receiver', function() {
var gotData = false;
var messages = [];
- p.on('text', function(data) {
+ p.ontext = function(data) {
gotData = true;
messages.push(data);
- });
+ };
for (var i = 0; i < packets.length; ++i) {
p.add(getBufferFromHexString(packets[i]));
@@ -91,13 +91,13 @@ describe('Receiver', function() {
var gotData = false;
var gotError = false;
var messages = [];
- p.on('text', function(data) {
+ p.ontext = function(data) {
gotData = true;
messages.push(data);
- });
- p.on('error', function(reason, code) {
+ };
+ p.onerror = function(reason, code) {
gotError = code == true;
- });
+ };
for (var i = 0; i < packets.length && !gotError; ++i) {
p.add(getBufferFromHexString(packets[i]));
@@ -116,12 +116,12 @@ describe('Receiver', function() {
var gotClose = false;
var gotError = false;
- p.on('close', function() {
+ p.onclose = function() {
gotClose = true;
- });
- p.on('error', function(reason, code) {
+ };
+ p.onerror = function(reason, code) {
gotError = code == true;
- });
+ };
for (var i = 0; i < packets.length && !gotError; ++i) {
p.add(getBufferFromHexString(packets[i]));
130 test/Receiver.test.js
View
@@ -3,42 +3,42 @@ var assert = require('assert')
require('should');
require('./hybi-common');
-describe('Receiver', function() {
+describe('Receiver', function() {
it('can parse unmasked text message', function() {
var p = new Receiver();
var packet = '81 05 48 65 6c 6c 6f';
-
+
var gotData = false;
- p.on('text', function(data) {
+ p.ontext = function(data) {
gotData = true;
assert.equal('Hello', data);
- });
-
+ };
+
p.add(getBufferFromHexString(packet));
gotData.should.be.ok;
});
it('can parse close message', function() {
var p = new Receiver();
var packet = '88 00';
-
+
var gotClose = false;
- p.on('close', function(data) {
+ p.onclose = function(data) {
gotClose = true;
- });
-
+ };
+
p.add(getBufferFromHexString(packet));
gotClose.should.be.ok;
});
it('can parse masked text message', function() {
var p = new Receiver();
var packet = '81 93 34 83 a8 68 01 b9 92 52 4f a1 c6 09 59 e6 8a 52 16 e6 cb 00 5b a1 d5';
-
+
var gotData = false;
- p.on('text', function(data) {
+ p.ontext = function(data) {
gotData = true;
assert.equal('5:::{"name":"echo"}', data);
- });
-
+ };
+
p.add(getBufferFromHexString(packet));
gotData.should.be.ok;
});
@@ -47,13 +47,13 @@ describe('Receiver', function() {
var message = 'A';
for (var i = 0; i < 300; ++i) message += (i % 5).toString();
var packet = '81 FE ' + pack(4, message.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68'));
-
+
var gotData = false;
- p.on('text', function(data) {
+ p.ontext = function(data) {
gotData = true;
assert.equal(message, data);
- });
-
+ };
+
p.add(getBufferFromHexString(packet));
gotData.should.be.ok;
});
@@ -64,11 +64,11 @@ describe('Receiver', function() {
var packet = '81 FF ' + pack(16, message.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68'));
var gotData = false;
- p.on('text', function(data) {
+ p.ontext = function(data) {
gotData = true;
assert.equal(message, data);
- });
-
+ };
+
p.add(getBufferFromHexString(packet));
gotData.should.be.ok;
});
@@ -80,13 +80,13 @@ describe('Receiver', function() {
var msgpiece2 = message.substr(150);
var packet1 = '01 FE ' + pack(4, msgpiece1.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece1, '34 83 a8 68'));
var packet2 = '80 FE ' + pack(4, msgpiece2.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece2, '34 83 a8 68'));
-
+
var gotData = false;
- p.on('text', function(data) {
- gotData = true;
+ p.ontext = function(data) {
+ gotData = true;
assert.equal(message, data);
- });
-
+ };
+
p.add(getBufferFromHexString(packet1));
p.add(getBufferFromHexString(packet2));
gotData.should.be.ok;
@@ -97,10 +97,10 @@ describe('Receiver', function() {
var packet = '89 ' + getHybiLengthAsHexString(message.length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68'));
var gotPing = false;
- p.on('ping', function(data) {
+ p.onping = function(data) {
gotPing = true;
assert.equal(message, data);
- });
+ };
p.add(getBufferFromHexString(packet));
gotPing.should.be.ok;
@@ -108,12 +108,12 @@ describe('Receiver', function() {
it('can parse a ping with no data', function() {
var p = new Receiver();
var packet = '89 00';
-
+
var gotPing = false;
- p.on('ping', function(data) {
+ p.onping = function(data) {
gotPing = true;
- });
-
+ };
+
p.add(getBufferFromHexString(packet));
gotPing.should.be.ok;
});
@@ -121,27 +121,27 @@ describe('Receiver', function() {
var p = new Receiver();
var message = 'A';
for (var i = 0; i < 300; ++i) message += (i % 5).toString();
-
+
var msgpiece1 = message.substr(0, 150);
var packet1 = '01 FE ' + pack(4, msgpiece1.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece1, '34 83 a8 68'));
-
+
var pingMessage = 'Hello';
var pingPacket = '89 ' + getHybiLengthAsHexString(pingMessage.length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(pingMessage, '34 83 a8 68'));
-
+
var msgpiece2 = message.substr(150);
var packet2 = '80 FE ' + pack(4, msgpiece2.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece2, '34 83 a8 68'));
-
+
var gotData = false;
- p.on('text', function(data) {
+ p.ontext = function(data) {
gotData = true;
assert.equal(message, data);
- });
+ };
var gotPing = false;
- p.on('ping', function(data) {
+ p.onping = function(data) {
gotPing = true;
assert.equal(pingMessage, data);
- });
-
+ };
+
p.add(getBufferFromHexString(packet1));
p.add(getBufferFromHexString(pingPacket));
p.add(getBufferFromHexString(packet2));
@@ -152,27 +152,27 @@ describe('Receiver', function() {
var p = new Receiver();
var message = 'A';
for (var i = 0; i < 300; ++i) message += (i % 5).toString();
-
+
var msgpiece1 = message.substr(0, 150);
var packet1 = '01 FE ' + pack(4, msgpiece1.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece1, '34 83 a8 68'));
-
+
var pingMessage = 'Hello';
var pingPacket = '89 ' + getHybiLengthAsHexString(pingMessage.length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(pingMessage, '34 83 a8 68'));
-
+
var msgpiece2 = message.substr(150);
var packet2 = '80 FE ' + pack(4, msgpiece2.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece2, '34 83 a8 68'));
-
+
var gotData = false;
- p.on('text', function(data) {
+ p.ontext = function(data) {
gotData = true;
assert.equal(message, data);
- });
+ };
var gotPing = false;
- p.on('ping', function(data) {
+ p.onping = function(data) {
gotPing = true;
assert.equal(pingMessage, data);
- });
-
+ };
+
var buffers = [];
buffers = buffers.concat(splitBuffer(getBufferFromHexString(packet1)));
buffers = buffers.concat(splitBuffer(getBufferFromHexString(pingPacket)));
@@ -188,15 +188,15 @@ describe('Receiver', function() {
var length = 100;
var message = new Buffer(length);
for (var i = 0; i < length; ++i) message[i] = i % 256;
- var originalMessage = getHexStringFromBuffer(message);
+ var originalMessage = getHexStringFromBuffer(message);
var packet = '82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68'));
var gotData = false;
- p.on('binary', function(data) {
+ p.onbinary = function(data) {
gotData = true;
assert.equal(originalMessage, getHexStringFromBuffer(data));
- });
-
+ };
+
p.add(getBufferFromHexString(packet));
gotData.should.be.ok;
});
@@ -205,15 +205,15 @@ describe('Receiver', function() {
var length = 256;
var message = new Buffer(length);
for (var i = 0; i < length; ++i) message[i] = i % 256;
- var originalMessage = getHexStringFromBuffer(message);
+ var originalMessage = getHexStringFromBuffer(message);
var packet = '82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68'));
var gotData = false;
- p.on('binary', function(data) {
+ p.onbinary = function(data) {
gotData = true;
assert.equal(originalMessage, getHexStringFromBuffer(data));
- });
-
+ };
+
p.add(getBufferFromHexString(packet));
gotData.should.be.ok;
});
@@ -222,15 +222,15 @@ describe('Receiver', function() {
var length = 200 * 1024;
var message = new Buffer(length);
for (var i = 0; i < length; ++i) message[i] = i % 256;
- var originalMessage = getHexStringFromBuffer(message);
+ var originalMessage = getHexStringFromBuffer(message);
var packet = '82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68'));
var gotData = false;
- p.on('binary', function(data) {
+ p.onbinary = function(data) {
gotData = true;
assert.equal(originalMessage, getHexStringFromBuffer(data));
- });
-
+ };
+
p.add(getBufferFromHexString(packet));
gotData.should.be.ok;
});
@@ -239,15 +239,15 @@ describe('Receiver', function() {
var length = 200 * 1024;
var message = new Buffer(length);
for (var i = 0; i < length; ++i) message[i] = i % 256;
- var originalMessage = getHexStringFromBuffer(message);
+ var originalMessage = getHexStringFromBuffer(message);
var packet = '82 ' + getHybiLengthAsHexString(length, false) + ' ' + getHexStringFromBuffer(message);
var gotData = false;
- p.on('binary', function(data) {
+ p.onbinary = function(data) {
gotData = true;
assert.equal(originalMessage, getHexStringFromBuffer(data));
- });
-
+ };
+
p.add(getBufferFromHexString(packet));
gotData.should.be.ok;
});
52 test/testserver.js
View
@@ -9,7 +9,7 @@ module.exports = {
handlers: {
valid: validServer,
invalidKey: invalidRequestHandler,
- closeAfterConnect: closeAfterConnectHandler
+ closeAfterConnect: closeAfterConnectHandler
},
createServer: function(port, handler, cb) {
if (handler && !cb) {
@@ -34,7 +34,7 @@ module.exports = {
*/
function validServer(server, req, socket) {
- if (typeof req.headers.upgrade === 'undefined' ||
+ if (typeof req.headers.upgrade === 'undefined' ||
req.headers.upgrade.toLowerCase() !== 'websocket') {
throw new Error('invalid headers');
return;
@@ -44,11 +44,11 @@ function validServer(server, req, socket) {
socket.end();
throw new Error('websocket key is missing');
}
-
+
// calc key
- var key = req.headers['sec-websocket-key'];
- var shasum = crypto.createHash('sha1');
- shasum.update(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
+ var key = req.headers['sec-websocket-key'];
+ var shasum = crypto.createHash('sha1');
+ shasum.update(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
key = shasum.digest('base64');
var headers = [
@@ -64,28 +64,28 @@ function validServer(server, req, socket) {
var sender = new Sender(socket);
var receiver = new Receiver();
- receiver.on('text', function (message, flags) {
+ receiver.ontext = function (message, flags) {
server.emit('message', message, flags);
sender.send(message);
- });
- receiver.on('binary', function (message, flags) {
+ };
+ receiver.onbinary = function (message, flags) {
flags = flags || {};
flags.binary = true;
server.emit('message', message, flags);
sender.send(message, {binary: true});
- });
- receiver.on('ping', function (message, flags) {
+ };
+ receiver.onping = function (message, flags) {
flags = flags || {};
server.emit('ping', message, flags);
- });
- receiver.on('pong', function (message, flags) {
+ };
+ receiver.onpong = function (message, flags) {
flags = flags || {};
server.emit('pong', message, flags);
- });
- receiver.on('close', function (code, message, flags) {
+ };
+ receiver.onclose = function (code, message, flags) {
flags = flags || {};
server.emit('close', code, message, flags);
- });
+ };
socket.on('data', function (data) {
receiver.add(data);
});
@@ -95,7 +95,7 @@ function validServer(server, req, socket) {
}
function invalidRequestHandler(server, req, socket) {
- if (typeof req.headers.upgrade === 'undefined' ||
+ if (typeof req.headers.upgrade === 'undefined' ||
req.headers.upgrade.toLowerCase() !== 'websocket') {
throw new Error('invalid headers');
return;
@@ -105,11 +105,11 @@ function invalidRequestHandler(server, req, socket) {
socket.end();
throw new Error('websocket key is missing');
}
-
+
// calc key
- var key = req.headers['sec-websocket-key'];
- var shasum = crypto.createHash('sha1');
- shasum.update(key + "bogus");
+ var key = req.headers['sec-websocket-key'];
+ var shasum = crypto.createHash('sha1');
+ shasum.update(key + "bogus");
key = shasum.digest('base64');
var headers = [
@@ -124,7 +124,7 @@ function invalidRequestHandler(server, req, socket) {
}
function closeAfterConnectHandler(server, req, socket) {
- if (typeof req.headers.upgrade === 'undefined' ||
+ if (typeof req.headers.upgrade === 'undefined' ||
req.headers.upgrade.toLowerCase() !== 'websocket') {
throw new Error('invalid headers');
return;
@@ -134,11 +134,11 @@ function closeAfterConnectHandler(server, req, socket) {
socket.end();
throw new Error('websocket key is missing');
}
-
+
// calc key
- var key = req.headers['sec-websocket-key'];
- var shasum = crypto.createHash('sha1');
- shasum.update(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
+ var key = req.headers['sec-websocket-key'];
+ var shasum = crypto.createHash('sha1');
+ shasum.update(key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11");
key = shasum.digest('base64');
var headers = [
Please sign in to comment.
Something went wrong with that request. Please try again.