Permalink
Browse files

Add more involved example of -S usage.

  • Loading branch information...
1 parent 71c7e87 commit b8fcded109ad01d38171c3a4e1d7278433b22698 @pgriess committed Aug 8, 2010
Showing with 40 additions and 9 deletions.
  1. +4 −1 README.md
  2. +6 −0 examples/echo/README.md
  3. +10 −0 examples/echo/server.js
  4. +10 −0 examples/echo/session.js
  5. +10 −8 wsbench
View
@@ -34,12 +34,15 @@ for each web socket opened, so we keep our counter in the module scope.
var cnt = 0;
module.exports = function(ws) {
- return function() {
+ ws.onopen = function() {
ws.send((++cnt <= 10) ? 'Hello' : 'world!');
ws.close();
};
};
+A more involved example is available in the `examples/echo/`
+[directory](http://github.com/pgriess/wsbench/tree/master/examples/echo/).
+
The complete usage is
usage: wsbench [options] <url>
View
@@ -0,0 +1,6 @@
+This example benchmarks an echo server: the client opens a connection, sends
+a message, waits for a message to be received, and then closes the
+connection. For this, we use the support for sessions via `wsbench -S`.
+
+The server requires a working installation of
+[node-websocket-server](http://github.com/miksago/node-websocket-server).
View
@@ -0,0 +1,10 @@
+var ws = require('websocket-server');
+
+var srv = ws.createServer();
+srv.addListener('connection', function(c) {
+ c.addListener('message', function(m) {
+ console.log(m);
+ c.write(m);
+ });
+});
+srv.listen(8000);
View
@@ -0,0 +1,10 @@
+module.exports = function(ws) {
+ ws.onopen = function() {
+ ws.send('biff');
+ };
+
+ ws.onmessage = function(m) {
+ console.log(m);
+ ws.close();
+ };
+};
View
18 wsbench
@@ -18,7 +18,7 @@ var OPTIONS = {
return a;
}();
- return function() {
+ ws.onopen = function() {
for (i = 0; i < OPTIONS.msgs; i++) {
var m = '';
var len = OPTIONS.msgSize;
@@ -124,13 +124,15 @@ if (!OPTIONS.rate) {
var createWS = function(src) {
var ws = new WebSocket(src, OPTIONS.protocol);
- ws.onopen = OPTIONS.session(ws);
+ OPTIONS.session(ws);
- ws.onclose = function() {
- if (++cnt < OPTIONS.connections) {
- createWS(src);
- }
- };
+ if (!ws.onclose) {
+ ws.onclose = function() {
+ if (++cnt < OPTIONS.connections) {
+ createWS(src);
+ }
+ };
+ }
}
createWS(OPTIONS.url);
@@ -148,7 +150,7 @@ if (!OPTIONS.rate) {
var ws = new WebSocket(OPTIONS.url, OPTIONS.protocol);
- ws.onopen = OPTIONS.session(ws);
+ OPTIONS.session(ws);
}, 1000);
}, i * (1000 / OPTIONS.rate));
}

0 comments on commit b8fcded

Please sign in to comment.