Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added w3c compatible CloseEvent for onclose / addEventListener("close…

…", ...
  • Loading branch information...
commit 46586c18c6e418379efc76b4890ead5ce0ad40a6 1 parent 9ced0b8
@einaros einaros authored
Showing with 55 additions and 5 deletions.
  1. +23 −5 lib/WebSocket.js
  2. +32 −0 test/WebSocket.test.js
View
28 lib/WebSocket.js
@@ -316,16 +316,21 @@ WebSocket.prototype.terminate = function() {
*/
WebSocket.prototype.addEventListener = function(method, listener) {
if (typeof listener === 'function') {
- // Special case for messages as we need to wrap the data
- // in a MessageEvent object.
if (method === 'message') {
function onMessage (data) {
listener.call(this, new MessageEvent(data));
}
-
- // store a reference so we can return the origional function again
+ // store a reference so we can return the original function from the addEventListener hook
onMessage._listener = listener;
this.on(method, onMessage);
+ }
+ else if (method === 'close') {
+ function onClose (code, message) {
+ listener.call(this, new CloseEvent(code, message));
+ }
+ // store a reference so we can return the original function from the addEventListener hook
+ onClose._listener = listener;
+ this.on(method, onClose);
} else {
this.on(method, listener);
}
@@ -343,7 +348,20 @@ module.exports = WebSocket;
function MessageEvent(dataArg) {
// Currently only the data attribute is implemented. More can be added later if needed.
- Object.defineProperty(this, 'data', { writable: false, value: dataArg });
+ this.data = dataArg;
+}
+
+/**
+ * W3C CloseEvent
+ *
+ * @see http://www.w3.org/TR/html5/comms.html
+ * @api private
+ */
+
+function CloseEvent(code, reason) {
+ this.wasClean = (typeof code == 'undefined' || code == 1000);
+ this.code = code;
+ this.reason = reason;
}
/**
View
32 test/WebSocket.test.js
@@ -1289,6 +1289,38 @@ describe('WebSocket', function() {
});
});
+ it('should receive valid CloseEvent when server closes with code 1000', function(done) {
+ var wss = new WebSocketServer({port: ++port}, function() {
+ var ws = new WebSocket('ws://localhost:' + port);
+ ws.addEventListener('close', function(closeEvent) {
+ assert.equal(true, closeEvent.wasClean);
+ assert.equal(1000, closeEvent.code);
+ ws.terminate();
+ wss.close();
+ done();
+ });
+ });
+ wss.on('connection', function(client) {
+ client.close(1000);
+ });
+ });
+
+ it('should receive vaild CloseEvent when server closes with code 1001', function(done) {
+ var wss = new WebSocketServer({port: ++port}, function() {
+ var ws = new WebSocket('ws://localhost:' + port);
+ ws.addEventListener('close', function(closeEvent) {
+ assert.equal(false, closeEvent.wasClean);
+ assert.equal(1001, closeEvent.code);
+ assert.equal('some daft reason', closeEvent.reason);
+ ws.terminate();
+ wss.close();
+ done();
+ });
+ });
+ wss.on('connection', function(client) {
+ client.close(1001, 'some daft reason');
+ });
+ });
});
describe('ssl', function() {
Please sign in to comment.
Something went wrong with that request. Please try again.