Skip to content

Commit

Permalink
More readme.
Browse files Browse the repository at this point in the history
  • Loading branch information
majek committed Aug 12, 2011
1 parent 5f9904d commit 3688777
Showing 1 changed file with 35 additions and 17 deletions.
52 changes: 35 additions & 17 deletions README.md
Expand Up @@ -6,33 +6,20 @@ To install `sockjs-node` run:
npm install sockjs


An echo SockJS server would look like:
An simplified echo SockJS server could look more or less like:

var http = require('http');
var sockjs = require('sockjs');

var sockjs_opts = {sockjs_url:
"http://majek.github.com/sockjs-client/sockjs-latest.min.js"};

var sjs = new sockjs.Server(sockjs_opts);
sjs.on('open', function(conn) {
var echo = new sockjs.Server(sockjs_opts);
echo.on('open', function(conn) {
conn.on('message', function(e) {
conn.send(e.data);
});
});


var server = http.createServer();
server.addListener('request', function(req, res) {
res.writeHead(404);
res.end('404 not found');
});
server.addListener('upgrade', function(req, con) {
con.end();
});

sjs.installHandlers(server, {prefix:'[/]echo'});

echo.installHandlers(server, {prefix:'[/]echo'});
server.listen(9999, '0.0.0.0');


Expand Down Expand Up @@ -137,6 +124,37 @@ For example:
});
});

### Footnote

A fully working echo server does need a bit more boilerplate (to
handle unanswered requests), here it is:

var http = require('http');
var sockjs = require('sockjs');

var sockjs_opts = {sockjs_url:
"http://majek.github.com/sockjs-client/sockjs-latest.min.js"};

var sjs = new sockjs.Server(sockjs_opts);
sjs.on('open', function(conn) {
conn.on('message', function(e) {
conn.send(e.data);
});
});

var server = http.createServer();
server.addListener('request', function(req, res) {
res.writeHead(404);
res.end('404 not found');
});
server.addListener('upgrade', function(req, con) {
con.end();
});

sjs.installHandlers(server, {prefix:'[/]echo'});

server.listen(9999, '0.0.0.0');

### Examples

If you want to see samples of running code, take a look at:
Expand Down

0 comments on commit 3688777

Please sign in to comment.