Permalink
Browse files

update receivers to avoid eventemitter

  • Loading branch information...
1 parent 9892d7b commit 8d5a6d9004ae5b53866d3e6dee0040171798ebbd @einaros einaros committed Jun 18, 2012
Showing with 152 additions and 149 deletions.
  1. +11 −12 lib/Receiver.hixie.js
  2. +20 −15 lib/Receiver.js
  3. +12 −13 lib/WebSocket.js
  4. +2 −2 lib/WebSocketServer.js
  5. +16 −16 test/Receiver.hixie.test.js
  6. +65 −65 test/Receiver.test.js
  7. +26 −26 test/testserver.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;
}
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();
}
}
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;
}
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
@@ -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]));
Oops, something went wrong.

0 comments on commit 8d5a6d9

Please sign in to comment.