Permalink
Browse files

Merge branch 'master' of github.com:einaros/ws

  • Loading branch information...
2 parents 8d0e8a5 + 1e50852 commit d76f7b9f61aa9137f5700986c12aefdee9336d65 @einaros einaros committed Dec 22, 2011
Showing with 116 additions and 4 deletions.
  1. +53 −2 lib/WebSocket.js
  2. +63 −2 test/WebSocket.test.js
View
55 lib/WebSocket.js
@@ -64,7 +64,7 @@ function WebSocket(address, options) {
/**
* Act as regular client
*/
-
+
this._isServer = false;
var serverUrl = url.parse(address);
@@ -317,7 +317,58 @@ WebSocket.prototype.terminate = function() {
else if (this._state == 'connecting') {
this._state = 'disconnected';
}
-}
+};
+
+/**
+ * Emulates the Browser based WebSocket interface.
+ *
+ * @see http://dev.w3.org/html5/websockets/#the-websocket-interface
+ * @api public
+ */
+
+['open', 'error', 'close', 'message'].forEach(function(method) {
+ Object.defineProperty(WebSocket.prototype, 'on' + method, {
+ /**
+ * Returns the current listener
+ *
+ * @returns {Mixed} the set function or undefined
+ * @api public
+ */
+
+ get: function get() {
+ var listener = this.listeners(method)[0];
+ return listener ? (listener._listener ? listener._listener : listener) : undefined;
+ },
+
+ /**
+ * Start listening for events
+ *
+ * @param {Function} listener the listener
+ * @returns {Mixed} the set function or undefined
+ * @api public
+ */
+
+ set: function set(listener) {
+ this.removeAllListeners(method);
+
+ if (typeof listener === 'function') {
+ // Special case for messages as we need to wrap the response here to
+ // emulate a WebSocket event response.
+ if (method === 'message') {
+ function message (data) {
+ listener.call(this, { data: data });
+ }
+
+ // store a reference so we can return the origional function again
+ message._listener = listener;
+ this.on(method, message);
+ } else {
+ this.on(method, listener);
+ }
+ }
+ }
+ });
+});
module.exports = WebSocket;
View
65 test/WebSocket.test.js
@@ -268,7 +268,7 @@ describe('WebSocket', function() {
assert.ok(error instanceof Error);
ws.terminate();
srv.close();
- done();
+ done();
});
});
})
@@ -599,7 +599,7 @@ describe('WebSocket', function() {
assert.ok(error instanceof Error);
ws.terminate();
srv.close();
- done();
+ done();
});
});
})
@@ -930,4 +930,65 @@ describe('WebSocket', function() {
});
})
})
+ describe('API emulation', function() {
+ it('should not throw errors when getting and setting', function(done) {
+ server.createServer(++port, function(srv) {
+ var ws = new WebSocket('ws://localhost:' + port);
+ var listener = function () {};
+
+ ws.onmessage = listener;
+ ws.onerror = listener;
+ ws.onclose = listener;
+ ws.onopen = listener;
+
+ assert.ok(ws.onopen === listener);
+ assert.ok(ws.onmessage === listener);
+ assert.ok(ws.onclose === listener);
+ assert.ok(ws.onerror === listener);
+
+ srv.close();
+ ws.terminate();
+ done();
+ });
+ });
+ it('should work the same as the EventEmitter api', function(done) {
+ server.createServer(++port, function(srv) {
+ var ws = new WebSocket('ws://localhost:' + port);
+ var listener = function() {};
+ var message = 0;
+ var close = 0;
+ var open = 0;
+
+ ws.onmessage = function(data) {
+ assert.ok(!!data.data);
+ ++message;
+ ws.close();
+ };
+
+ ws.onopen = function() {
+ ++open;
+ }
+
+ ws.onclose = function() {
+ ++close;
+ }
+
+ ws.on('open', function() {
+ ws.send('foo');
+ });
+
+ ws.on('close', function() {
+ process.nextTick(function() {
+ assert.ok(message === 1);
+ assert.ok(open === 1);
+ assert.ok(close === 1);
+
+ srv.close();
+ ws.terminate();
+ done();
+ });
+ })
+ });
+ });
+ })
})

0 comments on commit d76f7b9

Please sign in to comment.